2016-06-21 43 views
0

私は以下のコードを使用しました。JavascriptからApexに値を割り当てる方法

のVisualforceページのコードスニペット:

<Script> 
    //Window Load 
    document.getElementById("Today_Date").value = "2014-02-02"; 
</Script> 

<input type="date" value="{!myDate}" id="myDate"/> 
<apex:commandButton action="{!CallMyMethod}" value="End" > 

日が表示されますが、それは、アペックスに来ていません。

public date myDate{ get; set; } 

public PageReference CallMyMethod() { 
    //I got null when use the myDate; 
    return null; 
} 

答えて

1

HTML入力タグを使用しますが、代わりにapex:inputText tagが必要です。

したがって、apex:formでそれを使用すると、データをコントローラに送信できます。 inputTextにJSの値を入力するには、JSセレクターにアクセスするための入力にID属性を追加する必要があります(親コンポーネントにもIDが必要です)。ここでは簡単な例です:

Visualforceページ

<apex:page controller="TextInputController"> 
    <apex:form id="form"> 
     Input Text <apex:inputText value="{!inputText}" id="testText"/> 
     <apex:commandButton value="save" action="{!saveText}"/> 
    </apex:form> 
    <script> 
    document.getElementById('{!$Component.form.testText}').value ='Test value'; 
    </script> 
</apex:page> 

ページのcontroler

public with sharing class TextInputController{ 
    public String inputText{get;set;} 
    public PageReference saveText(){ 
     //process here text value 
     System.debug(inputText); 
     return null; 
    } 
} 
関連する問題