我想这样做,使用jQuery勾选复选框:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
这样的事情存在吗?
我想这样做,使用jQuery勾选复选框:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
这样的事情存在吗?
当前回答
这是完整的答案使用jQuery
我测试了它,它100%工作:D
// when the button (select_unit_button) is clicked it returns all the checed checkboxes values
$("#select_unit_button").on("click", function(e){
var arr = [];
$(':checkbox:checked').each(function(i){
arr[i] = $(this).val(); // u can get id or anything else
});
//console.log(arr); // u can test it using this in google chrome
});
其他回答
您还可以使用新方法扩展$.fn对象:
(function($) {
$.fn.extend({
check : function() {
return this.filter(":radio, :checkbox").attr("checked", true);
},
uncheck : function() {
return this.filter(":radio, :checkbox").removeAttr("checked");
}
});
}(jQuery));
然后你可以这样做:
$(":checkbox").check();
$(":checkbox").uncheck();
或者,如果您使用其他使用这些名称的库,您可能希望为它们提供更多的唯一名称,如mycheck()和myuncheck()。
如果您使用的是.prop('checked',true|false),并且没有更改复选框,则需要像这样添加触发器('click'):
// Check
$('#checkboxF1').prop( "checked", true).trigger('click');
// Uncheck
$('#checkboxF1').prop( "checked", false).trigger('click');
你可以的
$('.myCheckbox').attr('checked',true) //Standards compliant
or
$("form #mycheckbox").attr('checked', true)
如果在onclick事件中有要激发的复选框的自定义代码,请改用此代码:
$("#mycheckbox").click();
可以通过完全删除该属性来取消选中:
$('.myCheckbox').removeAttr('checked')
您可以这样选中所有复选框:
$(".myCheckbox").each(function(){
$("#mycheckbox").click()
});
这可能会帮助某人。
HTML5
<input id="check_box" type="checkbox" onclick="handleOnClick()">
JavaScript。
function handleOnClick(){
if($("#check_box").prop('checked'))
{
console.log("current state: checked");
}
else
{
console.log("current state: unchecked");
}
}
要使用jQuery 1.6或更高版本选中复选框,只需执行以下操作:
checkbox.prop('checked', true);
要取消选中,请使用:
checkbox.prop('checked', false);
下面是我喜欢使用jQuery切换复选框的内容:
checkbox.prop('checked', !checkbox.prop('checked'));
如果使用jQuery 1.5或更低版本:
checkbox.attr('checked', true);
要取消选中,请使用:
checkbox.attr('checked', false);