如何引用visualforce中指定的html元素id并传递给javascript函数
发布时间:2021-01-12 09:08:12 所属栏目:资源 来源:网络整理
导读:我有一个生成输入文本字段的顶点标记. apex:page id="my_page" apex:inputText id="foo" id="c_txt"/apex:inputText/apex:page 当有人点击此字段时,我想执行javascript. 但是当我检查HTML源代码时,这个成为输入标签的顶点标签有(我认为)动态生成的部分. inpu
我有一个生成输入文本字段的顶点标记. <apex:page id="my_page"> <apex:inputText id="foo" id="c_txt"></apex:inputText> </apex:page> 当有人点击此字段时,我想执行javascript. 但是当我检查HTML源代码时,这个成为输入标签的顶点标签有(我认为)动态生成的部分. <input type="text" size="50" value="Tue Nov 16 00:00:00 GMT 2010" name="j_id0:j_id3:j_id4:c_txt" id="j_id0:j_id3:j_id4:c_txt"> 正如你可以看到id有垃圾部分:( id="j_id0:j_id3:j_id4:c_txt" 在我的Javascript中,我正在尝试getElementById(‘c_txt’),但这当然不起作用.怎么处理这个? UPDATE 好像我可以做到这一点,但没有工作…… <apex:includeScript value="{!URLFOR($Resource.datepickerjs)}"></apex:includeScript> <apex:inputText id="foo" id="c_txt" onclick="javascript:displayDatePicker()" /> datepickerjs var elem = getElementById('c_txt'); alert(elem); 警报显示’null’,因此必定是错误的. 即使这个警报返回null … var targetDateField = document.getElementById('{!$Component.my_page:c_txt}'); alert(targetDateField); 解决方法您可以在javascript中使用$Component表示法,您可以这样使用它:var e = document.getElementById("{!$Component.ComponentId}"); 但要注意的一件事是,如果您的元素包含在具有ID的几个Visualforce标记级别中: <apex:pageBlock id="theBlock"> <apex:pageBlockSection id="theBlockSection"> <apex:commandLink action="{!someAction}" value="LINK!" id="theLink"/> // snip // in javascript you would reference this component using: document.getElementById("{!$Component.theBlock.theSection.theLink}"); (编辑:520站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |