我想这样做,使用jQuery勾选复选框:

$(".myCheckBox").checked(true);

or

$(".myCheckBox").selected(true);

这样的事情存在吗?


当前回答

如果您正在使用PhoneGap进行应用程序开发,并且您想立即显示按钮上的值,请记住执行此操作

$('span.ui-[controlname]',$('[id]')).text("the value");

我发现,如果没有跨度,无论你做什么,界面都不会更新。

其他回答

你可以试试这个:

$('input[name="activity[task_state]"]').val("specify the value you want to check ")

你可以的

$('.myCheckbox').attr('checked',true) //Standards compliant

or

$("form #mycheckbox").attr('checked', true)

如果在onclick事件中有要激发的复选框的自定义代码,请改用此代码:

$("#mycheckbox").click();

可以通过完全删除该属性来取消选中:

$('.myCheckbox').removeAttr('checked')

您可以这样选中所有复选框:

$(".myCheckbox").each(function(){
    $("#mycheckbox").click()
});

这将选择具有指定属性且值包含给定子字符串“ckbItem”的元素:

$('input[name *= ckbItem]').prop('checked', true);

它将选择名称属性中包含ckbItem的所有元素。

如果您考虑使用vanilla js而不是jquery,那么有一个解决方案:

//for one element: 
document.querySelector('.myCheckBox').checked = true /* or false */ //will select the first matched element
//for multiple elements:
for (const checkbox of document.querySelectorAll('.myCheckBox')) {
checkbox.checked = true //or false
}

2019年1月编辑

您可以使用:.prop(propertyName)-添加的版本:1.6

p{margin:20px 0 0;}b{颜色:红色;}标签{颜色:红色;}<!doctype html><html lang=“en”><head><meta charset=“utf-8”><script src=“https://code.jquery.com/jquery-1.10.2.js“></script></head><body><input id=“check1”type=“checkbox”checked=“checked”><label for=“check1”>检查此处</label><p></p><脚本>$(“input”).change(函数){var$input=$(this);$(“p”).html(“.attr(\”checked\“):<b>”+$input.attr“(”checked“)+”</b><br>“+“.prop(\”checked\“):<b>”+$input.prop(”checked“)+“</b><br>”+“.is(\”:checked \“):<b>”+$input.is(“:checked”)+“</b>”);}).change();</script></body></html>

论角度框架

示例1

在.html文件中

<input type="checkbox" (change)="toggleEditable($event)">

在.ts文件中

toggleEditable(event) {
     if ( event.target.checked ) {
         this.contentEditable = true;
    }
}

示例2

在.html文件中

<input type="checkbox" [(ngModel)]="isChecked" (change)="checkAction(isChecked ? 'Action1':'Action2')" />