设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

如何引用visualforce中指定的html元素id并传递给javascript函数

发布时间:2021-01-01 02:22 所属栏目:34 来源:网络整理
导读:我有一个生成输入文本字段的顶点标记. 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}");

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读