9

入力テキストフィールドを生成するapexタグがあります。

<apex:page id="my_page">
    <apex:inputText id="foo" id="c_txt"></apex:inputText>
</apex:page>

誰かがこのフィールドをクリックすると、javascriptを実行したいと思います。

しかし、HTMLソースを確認すると、入力タグとなるこのapexタグは、動的に生成された部分を持っていると思います。

   <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')いますが、これはもちろん機能しません。これに対処する方法???

アップデート

私はこれを行うことができるようですが、機能していません...

<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);
4

2 に答える 2

6

$Componentjavascriptで表記を使用でき、次のように使用します。

var e = document.getElementById("{!$Component.ComponentId}");

ただし、注意すべき点の1つは、要素が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}");
于 2011-12-16T09:39:32.190 に答える
5

私は自分の問題の解決策を得ました。

$ Compoentグローバルvisualforce式は、私の検索では、Javascript内ではなくvisualforceコードでのみ使用できます。

以下のコードは正常に機能します。inputTextフィールドの値をjsアラートメッセージに出力します。これで、id属性をJavascriptに渡して、必要なタスクを処理できます。

Created Date: <apex:inputText id="dah" value="{!created}" size="50" 
onclick="javascript:go('{!$Component.dah}')"></apex:inputText>

<script>
  function go(field) {
    var huh = document.getElementById(field).value;
    alert(huh); //returns the string u put inside of input text field
  }
</script>
于 2010-11-16T01:45:33.893 に答える