我有以下几点:
$(document).ready(function()
{
$("#select-all-teammembers").click(function() {
$("input[name=recipients\\[\\]]").attr('checked', true);
});
});
我希望id="select-all-teammembers"在选中和未选中之间切换。想法吗?那不是几十行代码吗?
我有以下几点:
$(document).ready(function()
{
$("#select-all-teammembers").click(function() {
$("input[name=recipients\\[\\]]").attr('checked', true);
});
});
我希望id="select-all-teammembers"在选中和未选中之间切换。想法吗?那不是几十行代码吗?
当前回答
在我看来,建议正常变种的人是GigolNet Gigolashvili,但我想建议更漂亮的变种。检查它
$(document).on('click', '.fieldWrapper > label', function(event) {
event.preventDefault()
var n = $( event.target ).parent().find('input:checked').length
var m = $( event.target ).parent().find('input').length
x = n==m? false:true
$( event.target ).parent().find('input').each(function (ind, el) {
// $(el).attr('checked', 'checked');
this.checked = x
})
})
其他回答
我认为触发点击更简单:
$("#select-all-teammembers").click(function() {
$("input[name=recipients\\[\\]]").trigger('click');
});
你可以这样写:
$(document).ready(function() {
$("#select-all-teammembers").click(function() {
var checkBoxes = $("input[name=recipients\\[\\]]");
checkBoxes.prop("checked", !checkBoxes.prop("checked"));
});
});
在jQuery 1.6之前,当我们只有attr()而没有prop()时,我们习惯这样写:
checkBoxes.attr("checked", !checkBoxes.attr("checked"));
但是prop()在应用于“布尔”HTML属性时具有比attr()更好的语义,所以在这种情况下通常是首选。
分别设置'checked'或null而不是true或false将完成这项工作。
// checkbox selection
var $chk=$(':checkbox');
$chk.prop('checked',$chk.is(':checked') ? null:'checked');
此代码将在单击web模板中使用的任何拨动开关动画器时拨动复选框。取代”。Onoffswitch-label”在你的代码中可用。 "checkboxID"是这里被切换的复选框。
$('.onoffswitch-label').click(function () {
if ($('#checkboxID').prop('checked'))
{
$('#checkboxID').prop('checked', false);
}
else
{
$('#checkboxID').prop('checked', true);
}
});
你也可以这样写
$(function() {
$("#checkbox-toggle").click(function() {
$('input[type=checkbox][name=checkbox_id\\[\\]]').click();
});
});
当用户单击id为“#checkbox-toggle”的按钮时,只需要调用复选框的单击事件。