开发者

getting values from javascript to apex code

I need to get a value from a javascript variable into a text field in a visualforce page. I got it using a command button.But I was wondering if there is any other way of getting it,cz i dun want an onclick event.

Th开发者_开发问答anks in advance


I can't tell by your comment what event you're listening for, but assuming you know what that will be, just use document.getElementById() or a selector in jQuery to get the input field. If you are using Apex:inputField, define the id attribute with something like 'theField'. When the page is rendered, Salesforce should give an id attribute like 'j_id0:j_id1:theField' to the real tag, but this can and probably will be different every time the page is viewed. That means you're going to need to select the input by a substring. Here's some sample code using jQuery (apologies to any jq gurus out there if it's inefficient-- feel free to improve).

<apex:page>
    <apex:includeScript value="{!$Resource.jquery_1_6_1}"/>
    <script>
        jQuery.noConflict();
        jQuery(document).ready(function() {
            jQuery('input[id*="theInput"]').val('Hello World');
        });
    </script>
    <apex:form >
        <apex:inputText value="{!phonenum}" id="theInput"/>
    </apex:form>
</apex:page>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜