JSP:一些表单字段是稳定的,而另一些则不稳定!

发布于 2024-11-02 02:47:46 字数 1369 浏览 0 评论 0原文

我想在选中复选框时启用输入字段。另外,我想对要填写的文本字段进行服务器端验证,如果选中该复选框,我面临的问题是,当我检查多个并提交表单的字段值为空,然后在 servlet 中进行验证并将结果返回给用户以填充输入,其中一个检查字段被检查,另一个则没有,即使我使用 param两者都稳定,不知道是什么原因!

这是 HTML 代码

 <div class="groupElement">

  <input type="checkbox" name="communicationWay" id="communicationWay2" value="emailOption" ${param.communicationWay == 'emailOption'?'checked':'' } onchange="chgtx('email','communicationWay2')"/>
  <label>Email</label>


     <input class="inpClass" id="email" type="text" name="emailComm" value="${param.emailComm}" disabled/>

</div>

 <div class="groupElement">

  <input type ="checkbox" name="communicationWay" id="communicationWay"  value="cellPhoneOption" ${param.communicationWay == 'cellPhoneOption'?'checked':''} onchange="chgtx('cellPhone','communicationWay')" />
   <label> Cell phone number</label>

    <input class="inpClass" id="cellPhone" type ="text" name="cellPhoneNoComm" value="${param.cellPhoneNoComm}" disabled />

     </div>

,这是在启用和禁用输入字段之间触发的 java 脚本函数

 function chgtx(field, checkbox) {

            var myField = document.getElementById(field);
            var checkBtton = document.getElementById(checkbox);
            myField.disabled= !checkBtton.checked;

        }

I want to enable the input field when check box is checked. Also, I want to make a server side validation of the text field, to be filled, if the check box is selected, the problem I face is that when I make check for more than one and submit the form with empty values of the filed then make validation in the servlet and return the result to the user to fill the inputs, one of the check fields is checked and the other is not, even I use param for filed stability for both, I don't know what is the reason!

Here's the HTML code

 <div class="groupElement">

  <input type="checkbox" name="communicationWay" id="communicationWay2" value="emailOption" ${param.communicationWay == 'emailOption'?'checked':'' } onchange="chgtx('email','communicationWay2')"/>
  <label>Email</label>


     <input class="inpClass" id="email" type="text" name="emailComm" value="${param.emailComm}" disabled/>

</div>

 <div class="groupElement">

  <input type ="checkbox" name="communicationWay" id="communicationWay"  value="cellPhoneOption" ${param.communicationWay == 'cellPhoneOption'?'checked':''} onchange="chgtx('cellPhone','communicationWay')" />
   <label> Cell phone number</label>

    <input class="inpClass" id="cellPhone" type ="text" name="cellPhoneNoComm" value="${param.cellPhoneNoComm}" disabled />

     </div>

and here's the java script function that trigger between enabling and disabling the input fields

 function chgtx(field, checkbox) {

            var myField = document.getElementById(field);
            var checkBtton = document.getElementById(checkbox);
            myField.disabled= !checkBtton.checked;

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-11-09 02:47:46

您的复选框元素具有相同的名称。因此,在服务器端,您必须使用 request.getParameterValues() 来获取所有检查的值。

String[] communicationWays = request.getParameterValues("communicationWay");
// ...

当您使用 request.getParameter() 时,例如 ${param} 所做的,只会返回一个值,即该组的第一个值。您可以使用 ${paramValues} 获取 EL 中的所有值。

然而,在 EL 中使用 ${paramValues} 来设置 checked 状态并不简单。有两种方法可以解决此问题:

  1. 创建一个自定义 EL 函数,执行类似的操作

    ${util:contains(paramValues.communicationWays, 'emailOption') ? “已检查”:“”}
    ${util:contains(paramValues.communicationWays, 'phoneOption') ? “已检查”:“”}
    

    public static boolean contains(Object[] array, Object value) {
        Arrays.sort(数组);
        返回 Arrays.binarySearch(数组, 值) > -1;
    }
    
  2. 在 servlet 中

    创建 Map 并在 EL 中使用它。

    Map<字符串,布尔值> communicationsWays = new HashMap();
    request.setAttribute("communicationWays", communicationsWays);
    
    for (String communicationsWay : request.getParameterValues("communicationWay")) {
        CommunicationWays.put(communicationWay, true);
    }
    

    <前><代码>${communicationWays.emailOption ? “已检查”:“”}
    ${communicationWays.phoneOption ? “已检查”:“”}

Your checkbox elements have the same name. So in the server side you have to use request.getParameterValues() to get all checked values.

String[] communicationWays = request.getParameterValues("communicationWay");
// ...

When you use request.getParameter(), such as ${param} is doing, only one value will be returned, which is the first one of the group. You can use ${paramValues} to get all values in EL.

However, using ${paramValues} in EL to set the checked state isn't exactly trivial. There are two ways to solve this:

  1. Create a custom EL function which does something like

    ${util:contains(paramValues.communicationWays, 'emailOption') ? 'checked' : ''}
    ${util:contains(paramValues.communicationWays, 'phoneOption') ? 'checked' : ''}
    

    with a

    public static boolean contains(Object[] array, Object value) {
        Arrays.sort(array);
        return Arrays.binarySearch(array, value) > -1;
    }
    
  2. Create a Map<String, Boolean> in servlet and use it instead in EL.

    Map<String, Boolean> communicationWays = new HashMap<String, Boolean>();
    request.setAttribute("communicationWays", communicationWays);
    
    for (String communicationWay : request.getParameterValues("communicationWay")) {
        communicationWays.put(communicationWay, true);
    }
    

    with

    ${communicationWays.emailOption ? 'checked' : ''}
    ${communicationWays.phoneOption ? 'checked' : ''}
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文