Javascript 复选框值真/假

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

Checkbox value true/false

javascriptjquerycheckbox

提问by divHelper11

I have a form with checkbox and I want to keep it checked after submitting the form when it goes back to the same view with an error. I heard that the value attribute can help me to make the checkbox be checked so im trying to set it to true/false. Anyway, the input value stays "false" even if I click it. What happens exactly? I thought the value goes true/false after clicking the checkbox

我有一个带有复选框的表单,当它返回到同一个视图并出现错误时,我想在提交表单后保持选中状态。我听说 value 属性可以帮助我选中复选框,所以我试图将其设置为 true/false。无论如何,即使我单击它,输入值也会保持“假”。究竟会发生什么?单击复选框后,我认为该值变为真/假

<input type="checkbox" name="acceptRules" class="inline checkbox" id="checkbox1" value="false">

      <script>
          $("#checkbox1").is(':checked', function(){
              $("#checkbox1").attr('value', 'true');
          });
      </script>

回答by Bojan Petkovski

If I understand the question, you want to change the value of the checkbox depending if it is checked or not.

如果我理解这个问题,您想根据是否选中来更改复选框的值。

Here is one solution:

这是一种解决方案:

$('#checkbox-value').text($('#checkbox1').val());

$("#checkbox1").on('change', function() {
  if ($(this).is(':checked')) {
    $(this).attr('value', 'true');
  } else {
    $(this).attr('value', 'false');
  }
  
  $('#checkbox-value').text($('#checkbox1').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<input type="checkbox" name="acceptRules" class="inline checkbox" id="checkbox1" value="false">

<div id="checkbox-value"></div>

回答by Aju John

Use Checked = true

Checked = true

$("#checkbox1").prop('checked', true);

Note: I am not clear whether you want to onclick/onchange event on checkbox. is(":checked", function(){})is a wrong in the question.

注意:我不清楚您是否要对复选框进行 onclick/onchange 事件。is(":checked", function(){})是一个错误的问题。

回答by Jitendra Tiwari

Try this

尝试这个

$("#checkbox1").is(':checked', function(){
  $("#checkbox1").prop('checked', true);
});
      
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="acceptRules" class="inline checkbox" id="checkbox1" value="false">

回答by Taplar

I'm going to post this answer under the following assumptions. 1) You (un)selected the checkbox on the first page and submitted the form. 2) Your building the second form and you setting the value="" true/false depending on if the previous one was checked. 3) You want the checkbox to reflect if it was checked or not before.

我将在以下假设下发布此答案。 1) You (un)selected the checkbox on the first page and submitted the form. 2) Your building the second form and you setting the value="" true/false depending on if the previous one was checked. 3) You want the checkbox to reflect if it was checked or not before.

If this is the case then you can do something like:

如果是这种情况,那么您可以执行以下操作:

var $checkbox1 = $('#checkbox1');
$checkbox1.prop('checked', $checkbox1.val() === 'true');

回答by Oluebube Princess Egbuna

To return true or false depending on whether a checkbox is checked or not, I use this in JQuery

要根据是否选中复选框返回真或假,我在 JQuery 中使用它

let checkState = $("#checkboxId").is(":checked") ? "true" : "false";

let checkState = $("#checkboxId").is(":checked") ? "true" : "false";

回答by Michael Ambrose

Checkboxes can be really weird in JS. You're best off checking for the presence of the checkedattribute. (I've had older jQuery versions return true even if checkedis set to 'false'.) Once you've determined that something is checked then you can get the value from the valueattribute.

JS 中的复选框可能真的很奇怪。您最好检查该checked属性是否存在。(即使checked设置为“false”,我也有较旧的 jQuery 版本返回 true 。)一旦您确定检查了某些内容,您就可以从value属性中获取值。

回答by Uzbekjon

jQuery.is()function does not have a signature for .is('selector', function).

jQuery.is()函数没有 的签名.is('selector', function)

I guess you want to do something like this:

我猜你想做这样的事情:

      if($("#checkbox1").is(':checked')){
          $("#checkbox1").attr('value', 'true');
      }