javascript 使用 form.serialize 获取正确的复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17809183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:42:59  来源:igfitidea点击:

Get proper checkbox value using form.serialize

javascriptjqueryserialization

提问by Theun Arbeider

We are currently using $('form').serialize()to get all form information

我们目前正在使用$('form').serialize()获取所有表单信息

This will return any checkbox values as "On" or "Off".

这将返回任何复选框值作为“开”或“关”。

Is there any way to get a 1/0 or true/false value using this same method?

有没有办法使用相同的方法获得 1/0 或 true/false 值?

回答by Konstantin Rudy

Yes. You can do it by adding a hidden input with the same name as checkbox which value will be submitted if checkbox is unchecked:

是的。您可以通过添加与复选框同名的隐藏输入来实现,如果未选中复选框,将提交该值:

<input type="hidden" name="option" value="false"/>
<input type="checkbox" name="option" value="true"/>

It will submit both values if checkbox is checked. But server side will read the latest one (value of checkbox)

如果复选框被选中,它将提交两个值。但是服务器端会读取最新的(复选框的值)

回答by RobG

The value of a form control is always a string. You can use radio buttons to get different values for the same control name:

表单控件的值始终是字符串。您可以使用单选按钮为相同的控件名称获取不同的值:

<form ...>
  <input type="radio" name="whatever" value="true">true
  <br>
  <input type="radio" name="whatever" value="false">false
  ...
</form>

Only one can be checked, and only the checked name/value will be sent to the server.

只能勾选一项,只有勾选的名称/值才会发送到服务器。

回答by Rajdeep

If we have multiple checkboxed in our page like :

如果我们的页面中有多个复选框,例如:

  <input id="reminder" name="Check1" value="false" type="checkbox" /> 

  <input id="alarm" name="Check2" value="false" type="checkbox" />

we have to bind the change event of these checkboxes and set its value to true or false.

我们必须绑定这些复选框的更改事件并将其值设置为 true 或 false。

 $('input[type=checkbox]').on('change', function(){
    var name =    $(this).attr('name');
    if($(this).is(':checked')){
    $('input[name= "' +name +'"]').val(true);
        $(this).val(true);
    }
    else{
       $(this).val(false);
       $('input[name= "' +name +'"]').val(false);
    }
});

By default, checkbox value doesn't appear in $('form').serializeArray(), It appears when it is checked.

默认情况下,复选框值不会出现在 $('form').serializeArray() 中,它在被选中时出现。

We have to keep one hidden field for each checkbox like :

我们必须为每个复选框保留一个隐藏字段,例如:

<input type="hidden" value="false" name="Check1" />
<input type="hidden" value="false" name="Check2" />

Now when we serialize the form now, we get the correct value like : Check1=true Check2=false

现在,当我们现在序列化表单时,我们会得到正确的值,例如: Check1=true Check2=false

回答by Petro Franko

You can add a new class for check boxes and set value when clicked.

您可以为复选框添加一个新类并在单击时设置值。

<input class="checkbox" id="someId" type="checkbox" value="false" />Checkbox

And use a small JQuery helper

并使用一个小的 JQuery 助手

$('.checkbox').click(function () {
    $(this).val() == "false" ? $(this).val("true") : $(this).val("false");
});

Then you will be able to get the value of the check box or serialize a form.

然后您将能够获取复选框的值或序列化表单。

回答by Rana

<input type="checkbox" name="check1" value="true" id="check1">

just set the value

只需设置值