[英]jQuery get checkbox value if checked and remove value when unchecked
jQuery function to get checkbox value if checked and remove value if unchecked. jQuery函数,如果选中则获取复选框值,如果未选中则删除值。
<input type="checkbox" id="check" value="3" />hiiii
<div id="show"></div>
function displayVals(){
var check = $('#check:checked').val();
$("#show").html(check);
}
var qqqq = window.setInterval( function(){
displayVals()
},
10
);
You don't need an interval, everytime someone changes the checkbox the change
event is fired, and inside the event handler you can change the HTML of #show
based on wether or not the checkbox was checked : 您不需要间隔,每次有人更改复选框时触发change
事件,并且在事件处理程序内部,您可以根据是否更改#show
的HTML来检查复选框:
$('#check').on('change', function() {
var val = this.checked ? this.value : '';
$('#show').html(val);
});
Working Demo http://jsfiddle.net/cse_tushar/HvKmE/5/ 工作演示http://jsfiddle.net/cse_tushar/HvKmE/5/
$(document).ready(function(){
$('#check').change(function(){
if($(this).prop('checked') === true){
$('#show').text($(this).attr('value'));
}else{
$('#show').text('');
}
});
});
there is this way too: 也有这样的方式:
Using the new property method will return true or false. 使用new属性方法将返回true或false。
$('#checkboxid').prop('checked'); $( '#checkboxid')丙( '检查');
Using javascript without libs 使用没有libs的javascript
document.getElementById('checkboxid').checked 的document.getElementById( 'checkboxid')。检查
Using the JQuery's is() 使用JQuery是()
$("#checkboxid").is(':checked') $( “#checkboxid”)是。( ':勾选')
Using attr to get checked 使用attr进行检查
$("#checkboxid").attr("checked") $( “#checkboxid”)。ATTR( “选中”)
i prefer second option. 我更喜欢第二种选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.