简体   繁体   中英

Javascript to enable, disable drop down menu in JSP file having Java code only, no HTML directly

Let me introduce the design.

Each field in a JSP is made by a Map . The label name, the input type (drop down or other), the input values, the default values, etc are added to the Map , say Map nameAttributes . There is another class, GeneralWriter writer , to which I do not have access, which takes the values from Map , parses them and writes the proper HTML code.

After writing the Map, writer.writeSelectBox(nameAttributes); is called.

Now, the requirement is:
There is a drop-down menu, depending upon its selected value, some other drop-down menus are disabled (shown in UI, but not modifiable) or enabled. Since, I am not writing HTML code for the added field, I can not write the function call events to do my job.

I have observed that a JS function is called on onMouseOut event from the fields, as seen in "View Source" . So I thought I might write my code there to check the field value and impact other drop-down menus. But if I write alert in the JS function (just to check), it won't alert me, means the function is not called and I can not write the enable/disbale code.

Is there any way to achieve the job. The enabling and disabling should depend on what user selects in one of the drop downs.

Sample code:

<%
Map nameAttr = new HashMap();   
nameAttr.put(GeneralConst.INPUT_MESSAGE, Const.MSG_FIELD_NAME);
//.....

writer.writeAllSelectBox(nameAttr);
%>

Urgent help needed, thanks.

In View Source of the JSP page, its clear that for some fields onChange and onMouseOut events etc. were added, and in the GeneralWriter class, those things were being written out to the out stream as follows

out.println("<td nowrap><select name=" + strName);
out.println(" style=\"width:" + strWidth + "px\"");
out.println(" onChange ="+onChange);

So, I added my own method there rather than using existing code and introduced the onChange event. The " onChange " variable that you see (the right one) is of java.lang.String type and this had to be passed from the JSP itself, as to what behavior I want or which particular function to call for a particular JSP.

Now I added the following in JSP

<body>
  <script>
    window.onload = setModeForPara2('MEMBERTYPE','MEMBERSTATE','CKSTATUS') ;
    function resetevent(uri){
      setModeForPara2('MEMBERTYPE','MEMBERSTATE','CKSTATUS') ;
    }
  </script>
</body>

where the parameters are the names of the drop down menus, and setModeForPara2() is a JS function to disable/enable as my requirement was stated in original post. It checks if MEMBERTYPE is 0, then render disabled state and gray color to MEMBERSTATE and CKSTATUS drop-down menus, else enable them and remove gray color.
The resetevent(uri) is also a JS function called when the Reset button is clicked to render the disabled state to the State and Status fields.

So that way, setModeForPara2() function is called whenever:
1. the page is loaded,
2. the value of first drop-down (MEMBERTYPE) is changed, and
3. the Reset button is clicked.

Resolved.

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