[英]How can i process value get from checkbox by JS then do something with it in Java action?
I have a checkbox like this in JSP我在 JSP 中有一个这样的复选框
<input type="checkbox" id="checkBox" name="checkBox"/>
In JS file I wrote a function to set data for a variable when checkbox be clicked在 JS 文件中,我编写了一个函数来在单击复选框时为变量设置数据
$("checkBox").change(function(){
var checkBox= 0;
if(this.checked) {
checkBox= 1;
} else {
checkBox= 0;
}
})
Then in Java action class I want to do something when checkbox be clicked然后在 Java 操作类中,我想在单击复选框时执行某些操作
if(bean.getCheckBox == 1) {//do something}
But it's not working.但它不起作用。 Please help me fix this one !请帮我解决这个问题! thanks谢谢
The var checkBox
is a client-side JavaScript local variable and its value will only ever be visible inside the change
event listener; var checkBox
是一个客户端 JavaScript本地变量,它的值只会在change
事件侦听器中可见; your action class which (I'm assuming) is a server side component will not have access to this value.您的操作类(我假设)是服务器端组件将无法访问此值。
Your action class should instead check for the value of the checkBox
request parameter which comes from the value
attribute.您的操作类应该检查来自value
属性的checkBox
请求参数的value
。
<input type="checkbox" id="checkBox" name="checkBox" value="1" />
Then in your action class you'll have something like:然后在您的操作类中,您将拥有如下内容:
if ("1".equals(request.getParameter("checkBox"))) {
...
}
Or, you can also simply do a null-check and not check for any specific value.或者,您也可以简单地进行空检查而不检查任何特定值。
if (request.getParameter("checkBox") != null) {
...
}
If you're using some framework bean, you'll use something like below depending on the bean's checkBox
variable's data type.如果您正在使用某个框架 bean,您将根据 bean 的checkBox
变量的数据类型使用如下所示的内容。
if ("1".equals(bean.getCheckBox())) { // String
...
}
// or
if (bean.getCheckBox() != null) { // String or Integer
...
}
// or
if (bean.getCheckBox() == 1) { // int primitive
...
}
The value to check for still comes from the value
attribute above.要检查的值仍然来自上面的value
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.