我的复选框传递值甚至未选中 - Grails [英] my checkbox pass value even unchecked - Grails
问题描述
表单
中有很多复选框
元素,其中一个是: < g:checkBox id =巩固name =巩固value =$ {true}checked =$ {false}/ >
然后在接收控制器
复选框
使用此代码
的值:
println params。.consolidate
它显示:
关于
不管我是否勾选了我的复选框。在其他语言中,如果复选框未勾选,则其在控制器上的值将为 null
或 undefined
。什么是未经检查的价值,以及在grails控制器上访问其价值的正确代码是什么?
临时解决方案:
以下代码(位于 JavaScript
)是我暂时用来容纳我的需求的。尽管我想要的是对此行为的解释或更正。
var serialized_string =;
$(#form input)。each(function(i,j){
var o = $(j);
if(o.val()!== undefined& ;& o.val()!==undefined&& o.val()!==){
if(serialized_string ===){
serialized_string + = o.attr(name)+=;
}
else {
if(o.attr(name)===巩固){
var val =false;
if(o.prop(checked)){
val =true;
}
data + = val;
else {
data + =&+ o.attr(name)+=;
}
}
}
});
如果选中,您会看到 / strong>你看到了;否则您会看到 null ,它是 false 。
I have lots of checkbox
element on my Grails form
, one is this:
<g:checkBox id="consolidate" name="consolidate" value="${true}" checked="${false}" />
Then on the receiving controller
, I verify the value of the checkbox
using this code
:
println params?.consolidate
And it displays:
on
Regardless whether I've tick my checkbox or not. In other language, if the checkbox is not ticked, its value on the controller will be null
or undefined
. What should be its value when unchecked, and what is the right code to access its value on the grails controller?
Temporary Solution:
The following code (on JavaScript
) is what I had used temporarily to accommodate my requirement. Though what I want is an explanation or maybe correction about this behavior.
var serialized_string = "";
$("#form input").each(function(i, j) {
var o = $(j);
if(o.val() !== undefined && o.val() !== "undefined" && o.val() !== "") {
if(serialized_string === "") {
serialized_string += o.attr("name") + "=";
}
else {
if(o.attr("name") === "consolidate") {
var val = "false";
if(o.prop("checked")) {
val = "true";
}
data += val;
}
else {
data += "&" + o.attr("name") + "=";
}
}
}
});
If checked, you see the on you're seeing; otherwise you see null, which is your false.
这篇关于我的复选框传递值甚至未选中 - Grails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!