我的复选框传递值甚至未选中 - Grails [英] my checkbox pass value even unchecked - Grails

查看:127
本文介绍了我的复选框传递值甚至未选中 - Grails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆