javascript 选中和取消选中时如何为html中的复选框赋值?

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

How to assign values to checkbox in html when check and uncheck?

javascriptjqueryhtmlforms

提问by Sameera Liaynage

I have a html checkbox in my html form. Now, i want to assign 1 when it is checked and assign 0 when it is unchecked. So, how can i do that with jquery or java Script?

我的 html 表单中有一个 html 复选框。现在,我想在选中时分配 1,在未选中时分配 0。那么,我如何使用 jquery 或 java Script 做到这一点?

<!DOCTYPE html>
<html>

  <script language="javascript" type="text/javascript">
  <!--
  function greeter() {
      alert(document.getElementById(vehicleChkBox).value);
  }
  $('#vehicleChkBox').change(function(){
       if($(this).attr('checked')){
            $(this).val(1);
       }else{
            $(this).val(0);
       }
  });
  </script>
  <body>
    <form>
      <input type="checkbox" name="vehicle" id="vehicleChkBox" value="FALSE" />
      <input type="submit" onsubmit="greeter()">
    </form>
  </body>

I tried with this. but, does not work.

我试过这个。但是,不起作用。

回答by T J

JS:

JS:

var checkbox = document.querySelector("input[type='checkbox']");
  checkbox.addEventListener("change",function(){
     this.value = this.checked ? 1 : 0;
  });

jQuery:

jQuery:

$(":checkbox").change(function(){
     $(this).val($(this).is(":checked") ? 1 : 0);
});

回答by T J

You may use JQuery for this with changeevent:

您可以将 JQuery 用于此change事件:

$(function() {
  $('#vehicleChkBox').on('change', function(e) {
    e.stopPropagation();
    this.value = this.checked ? 1 : 0;
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="vehicle" id="vehicleChkBox" value="0" />

回答by David Jones

You simply need to put its value as 1. This way when it is checked it will return 1 and when it is not checked it will return nothing so that way you can do a check and assign zero.

你只需要把它的值设为 1。这样,当它被检查时它会返回 1,当它不被检查时它不会返回任何东西,这样你就可以进行检查并分配零。

<input type="checkbox" name="my_checkbox" value="1" />

回答by Amol

I am using this and this is working absolutely fine:

我正在使用这个,这工作得很好:

$("#checkkBoxId").attr("checked") ? alert("Checked") : alert("Unchecked");

Note: If the checkbox is checked it will return true otherwise undefined, so better check for the "TRUE" value.

注意:如果复选框被选中,它将返回真否则未定义,所以最好检查“真”值。

OR

或者

if(!$("#checkkBoxId").is(':checked') ){
 $(this).val('1');
}
else
$(this).val('0');

回答by A.B

This will work fine for setting value

这将适用于设置值

   $("#checkkBoxId").prop("checked") ? ($("#checkkBoxId").val(1)) :    ($("#checkkBoxId").val(0));

回答by herr

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script> 
   $(document).ready(function() {
         $('#vehicleChkBox').click(function(){
             if($(this).is(':checked'))
             {              
                  $(this).val('1');
             }
             else
             {
                 $(this).val('0');
             }
         });
    });
</script>