Reputation: 5
how to post value of checkbox when it checked or uncheked
I have Code
$("#check").click(function(){`var data = {
kd_material:$("#kd_material").val(),
check : $(this).val('1') ? $(this).val("1") : $(this).val("0")
};
$.ajax({
type: "POST",
url : "<?php echo base_url().'ms_select/select_id_material_koreksi_cek'?>",
data: data,
success: function(msg){
$('#div-gudang').html(msg);
}
});
}); `
Upvotes: 0
Views: 50
Reputation: 26288
Try this:
$("#check").click(function(){
if($(this).is(':cheked'))
{
var chkValue = 1;
}
else
{
var chkValue = 0;
}
});
Use chkValue as a variable in ajax() data
like:
data: {
value : chkValue
}
Upvotes: 0
Reputation: 782499
$(this).val('1')
doesn't return a boolean. It sets the value of $(this)
to 1
and returns $(this)
for chaining.
To tell whether the box is checked, use this.checked
or $(this).is(":checked")
. So it should be:
check: this.checked ? 1 : 0
Upvotes: 2
Reputation: 2613
You can simply use the isChecked property of the checkbox and send it.
$("#check").click(function(){`var data = {
kd_material:$("#kd_material").val(),
check : $(this).is(":checked")
};
The value true/false would be sent to the server.
But if you want to send 1/0 you can do it like this.
check : $(this).is(":checked") ? 1 : 0
Upvotes: 0