简体   繁体   中英

set value for java variable from javascript function

I have a aui select field(drop-down), & I want to perform some operation if drop-down option changes.

So my aui select field shows like this:

<form action="<liferay-portlet:actionURL name="saveInfo" />"

    <aui:select name="selectAmount" inlineLabel="true" label="select-amount" onChange="performOperationsOnInputField()">
        <aui:option value="0"></aui:option>
        <aui:option value="1">1</aui:option>
        <aui:option value="2">2</aui:option>
        <aui:option value="3">3</aui:option>
        <aui:option value="4">4</aui:option>
        <aui:option value="5">5</aui:option>
    </aui:select>

</form> 

and script is :

<script type="text/javascript">
     function performOperationsOnInputField() {
         var portletNamespace = '<portlet:namespace />';
         var selectedValue = $('#selectAmount').val();

         if(selectedValue == '0') {
            // set a java variable value which can be access fro any java file(like static variable) or any best way so that we can access it from any java/jsp file

            });                   
        } 
    }
</script>   

Can we set a java variable value from script so that we can access value of this fields globally from any java file & from any jsp page?

// set a java variable value which can be access fro any java file(like static variable) or any best way so that we can access it from any java/jsp file

No you cannot.

You might misunderstand that JSP and JavaScript existed on same document. Yes but JSP part compiles on server side itself comes to client .

Javascript Plays on client side and JSP plays on server side.

What you need to do is you have to make a server request. And send that string as a query parameter or form or Ajax .

对服务器进行基于Ajax的调用会将您的数据从Java脚本传递到Java。

您可以使用基于ajax的调用,并且可以通过将值追加到queryString中来传递这些值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM