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
6 Answers
Reset to default 4JS:
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
版权声明:本文标题:javascript - How to assign values to checkbox in html when check and uncheck? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741213477a2359589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论