admin管理员组

文章数量:1279187

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?

<!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.

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?

<!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.

Share Improve this question edited Sep 29, 2014 at 11:33 Sameera Liaynage asked Sep 29, 2014 at 11:27 Sameera LiaynageSameera Liaynage 8412 gold badges10 silver badges12 bronze badges 2
  • Please post ur code and what you have tried so far! – Nitin Varpe Commented Sep 29, 2014 at 11:27
  • Assign to value of checkbox. – Sameera Liaynage Commented Sep 29, 2014 at 11:31
Add a ment  | 

6 Answers 6

Reset to default 4

JS:

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

jQuery:

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

You may use JQuery for this with change event:

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

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.

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

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');
<script src="http://code.jquery./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>

This will work fine for setting value

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

本文标签: javascriptHow to assign values to checkbox in html when check and uncheckStack Overflow