使用 jQuery 将复选框的值设置为 true 或 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8328033/
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-08-27 10:21:48  来源:igfitidea点击:

Setting the value of checkbox to true or false with jQuery

jquery

提问by Rene Zammit

I have a form with a checkbox. With jQuery I would like to set the value of the checkbox to TRUE if checked, and if it is not checked, the value will be set to FALSE. How I can do this please?

我有一个带有复选框的表单。使用jQuery,如果选中,我想将复选框的值设置为TRUE,如果未选中,则该值将设置为FALSE。请问我该怎么做?

回答by Nicola Peluchetti

You can do (jQuery 1.6 onwards):

你可以这样做(jQuery 1.6 起):

$('#idCheckbox').prop('checked', true);
$('#idCheckbox').prop('checked', false);

to remove you can also use:

要删除,您还可以使用:

$('#idCheckbox').removeProp('checked');

with jQuery < 1.6 you must do

使用 jQuery < 1.6 你必须这样做

$('#idCheckbox').attr('checked', true);
$('#idCheckbox').removeAttr('checked');

回答by Jose Vega

UPDATED: Using prop instead of attr

更新:使用 prop 而不是 attr

 <input type="checkbox" name="vehicle" id="vehicleChkBox" value="FALSE"/>

 $('#vehicleChkBox').change(function(){
     cb = $(this);
     cb.val(cb.prop('checked'));
 });

OUT OF DATE:

过时

Here is the jsfiddle

这是jsfiddle

<input type="checkbox" name="vehicle" id="vehicleChkBox" value="FALSE" />

$('#vehicleChkBox').change(function(){
     if($(this).attr('checked')){
          $(this).val('TRUE');
     }else{
          $(this).val('FALSE');
     }
});

回答by ThiefMaster

Use $('#id-of-the-checkbox').prop('checked', true_or_false);

$('#id-of-the-checkbox').prop('checked', true_or_false);

In case you are using an ancient jQuery version, you'll need to use .attr('checked', 'checked')to check and .removeAttr('checked')to uncheck.

如果您使用的是古老的 jQuery 版本,则需要使用.attr('checked', 'checked')来选中和.removeAttr('checked')取消选中。

回答by Krule

Try this:

尝试这个:

HTML:

HTML:

<input type="checkbox" value="FALSE" />

jQ:

简问:

$("input[type='checkbox']").on('change', function(){
  $(this).val(this.checked ? "TRUE" : "FALSE");
})

jsfiddle

提琴手

Please bear in mind that unchecked checkbox will not be submitted in regular form, and you should use hidden filed in order to do it.

请记住,未选中的复选框不会以常规形式提交,您应该使用隐藏文件来提交。

回答by Esailija

var checkbox = $( "#checkbox" );
checkbox.val( checkbox[0].checked ? "true" : "false" );

This will set the valueof the checkbox to "true"or "false"(value propertyis a string), depending whether it's uncheckedor checked.

这会将value复选框的设置为"true"or "false"(value property是 a string),取决于它是unchecked还是checked

Works in jQuery >= 1.0

适用于 jQuery >= 1.0

回答by Gothic345

<input type="checkbox" name="vehicle" id="vehicleChkBox" value="FALSE"/>

--

——

$('#vehicleChkBox').change(function(){
    if(this.checked)
        $('#vehicleChkBox').val('TRUE');
   else
        $('#vehicleChkBox').val('False');
});