简体   繁体   English

启用Java的Javascript,禁用仅具有Java代码,无HTML的JSP文件中的下拉菜单

[英]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 . JSP中的每个字段都由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 . 标签名称,输入类型(下拉菜单或其他),输入值,默认值等被添加到Map ,例如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. 还有另一个类GeneralWriter writer ,我没有访问权限,该类从Map获取值,解析它们并编写适当的HTML代码。

After writing the Map, writer.writeSelectBox(nameAttributes); 写入地图后, 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. 有一个下拉菜单,取决于其选定的值,某些其他下拉菜单被禁用(在UI中显示,但不可修改)或启用。 Since, I am not writing HTML code for the added field, I can not write the function call events to do my job. 由于我没有为添加的字段编写HTML代码,因此我无法编写函数调用事件来完成我的工作。

I have observed that a JS function is called on onMouseOut event from the fields, as seen in "View Source" . 我观察到,在字段上的onMouseOut事件上调用了JS函数,如"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. 但是,如果我在JS函数中编写alert (只是检查),它不会警报我,这意味着未调用该函数,并且我无法编写启用/禁用代码。

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 在JSP页面的“查看源代码”中,很明显,已为某些字段添加了onChange和onMouseOut事件等,并在GeneralWriterGeneralWriter这些内容写出到输出流中,如下所示

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. 因此,我在这里添加了自己的方法,而不是使用现有的代码,并介绍了onChange事件。 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. 您看到的(正确的)“ onChange ”变量是java.lang.String类型的,这必须从JSP本身传递,关于我想要什么行为或为特定JSP调用哪个特定函数。

Now I added the following in JSP 现在我在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. 其中参数是下拉菜单的名称,而setModeForPara2()是要禁用/启用的JS函数,如我在原始帖子中所述。 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. 它检查MEMBERTYPE是否为0,然后将禁用状态和灰色呈现给MEMBERSTATE和CKSTATUS下拉菜单,否则启用它们并删除灰色。
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. 当单击“重置”按钮以将禁用状态呈现给“状态”和“状态”字段时, resetevent(uri)也是一个JS函数。

So that way, setModeForPara2() function is called whenever: 因此,只要有以下情况, setModeForPara2()调用setModeForPara2()函数:
1. the page is loaded, 1.页面已加载,
2. the value of first drop-down (MEMBERTYPE) is changed, and 2.第一个下拉菜单(MEMBERTYPE)的值已更改,并且
3. the Reset button is clicked. 3.单击重置按钮。

Resolved. 解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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