2016-06-21 24 views
0

我已經使用了下面的代碼。如何從Javascript中將值分配給頂點

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上使用它,您將能夠將數據提交給控制器。爲了用JS中的值填充inputText,你需要添加ID屬性來訪問它的輸入JS選擇器(父組件也需要一個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; 
    } 
}