How To Set Value Of Checkbox When Checked And Unchecked Using Ajax Or Jquery
I need to get the value of the checkboxes when checked and unchecked and save it in database. The value is 1 if checked and 0 if unchecked, respectively. I am currently using the s
Solution 1:
Probably you want this:
$('input[type="checkbox"]').click(function () {
$(this).prop("checked") ? $(this).val("1") : $(this).val("0")
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox"value="1"name="one_by_one"id="one_by_one"class="one_by_one">One
Solution 2:
The value of a checkbox never changes (by itself). If the checkbox is not checked its value simply won't be sent to the server. So at the server side you should check whether one_by_one
is included in the request and set the value accordingly (e.g. set 0
if it doesn't exist or the value of one_by_one
if it does).
Post a Comment for "How To Set Value Of Checkbox When Checked And Unchecked Using Ajax Or Jquery"