2016-06-21 3 views
0

Я использовал приведенный ниже код.Как присвоить значение Apex из 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" > 

дата виден, но он не приходит к Apex.

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 вам нужно добавить атрибут 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> 

Page Контроллер:

public with sharing class TextInputController{ 
    public String inputText{get;set;} 
    public PageReference saveText(){ 
     //process here text value 
     System.debug(inputText); 
     return null; 
    } 
} 
Смежные вопросы