gpt4 book ai didi

javascript - jQuery 选择使用 prop 或 attr 时不起作用的所有复选框

转载 作者:行者123 更新时间:2023-11-28 20:21:34 24 4
gpt4 key购买 nike

我正在尝试使用 jQuery 检查表单中的所有复选框,但它不起作用。我使用以下代码将其绑定(bind)到一些链接元素:

$(document).ready(function() {

$('#discounts_all').click(function() {
$('.discount_select').prop('checked', true);
});

$('#discounts_none').click(function() {
$('.discount_select').prop('checked', false);
});

}

我也尝试过:

$(document).ready(function() {

$('#discounts_all').click(function() {
$('.discount_select').attr('checked', true);
});

$('#discounts_none').click(function() {
$('.discount_select').attr('checked', false);
});

}

元素的 html:

<a href="javascript:void(0);" id="discounts_all">All</a>
<a href="javascript:void(0);" id="discounts_none">None</a>

但是,当我单击它们时,什么也没有发生,没有错误或任何东西。

最佳答案

至少一个主要问题是您错过了 ready 处理程序的右括号:);(请参阅评论)。

<小时/>

另外,我会使用 return false; 而不是 javascript:void(0):

$(document).ready(function() {

$('#discounts_all').click(function() {
$('.discount_select').prop('checked', true);
return false;
});

$('#discounts_none').click(function() {
$('.discount_select').prop('checked', false);
return false;
});

}); // <<< Also you miss this: ");"

和 html:

<a href="#" id="discounts_all">All</a>
<a href="#" id="discounts_none">None</a>

JSDIDDLE

关于javascript - jQuery 选择使用 prop 或 attr 时不起作用的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18216136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com