JSP:一些表单字段是稳定的,而另一些则不稳定!
我想在选中复选框时启用输入字段。另外,我想对要填写的文本字段进行服务器端验证,如果选中该复选框,我面临的问题是,当我检查多个并提交表单的字段值为空,然后在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的复选框元素具有相同的名称。因此,在服务器端,您必须使用 request.getParameterValues() 来获取所有检查的值。
当您使用
request.getParameter()
时,例如${param}
所做的,只会返回一个值,即该组的第一个值。您可以使用${paramValues}
获取 EL 中的所有值。然而,在 EL 中使用
${paramValues}
来设置checked
状态并不简单。有两种方法可以解决此问题:创建一个自定义 EL 函数,执行类似的操作
与
创建
Map
并在 EL 中使用它。与
<前><代码>${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.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 thechecked
state isn't exactly trivial. There are two ways to solve this:Create a custom EL function which does something like
with a
Create a
Map<String, Boolean>
in servlet and use it instead in EL.with