如果我的六个复选框之一未选中,则禁用锚 link '<a>'
disable anchor link '<a>' if one of my six checkboxes are not checked
我怎样才能禁用一个锚点link 如果我的六 (6) 个复选框中有一个 (1) 未选中?
var first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094');
$("a").click(function(e) {
if($("first_option").prop("checked") === false) {
e.preventDefault(); return false;
} else {return true;};
});
first_option.prop("checked")
将始终检查第一个元素。您需要做的是遍历所有元素以检查
像这样
$("#tmp_button-99035").click(function(e) {
var isChecked = false;
for (var i = 0; i < first_option.length; i++) {
if (first_option.eq(i).prop("checked")) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert('Please Choose Collar Colour To Continue');
e.preventDefault();
}
return isChecked;
});
您当前的逻辑不起作用,因为您只查看第一个元素的 checked
属性 select,而不是所有元素。
要实现您的要求,您可以使用 :checked
selector 获取您提供的 select 中的所有选中元素,然后检查 length
属性 的结果,看看有没有。试试这个:
var $first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094');
$("#tmp_button-99035").click(function(e) {
if ($first_option.filter(':checked').length === 0) {
e.preventDefault();
alert('Please Choose Collar Colour To Continue');
};
});
好吧,你的 js 片段只检查第一个元素。因此,您还必须跟踪其他复选框以获得正确的结果。
var first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094')
$(document).on('click', '#tmp_button-99035', function (e) {
if ($(first_option).filter(":checked").length == 0) {
e.preventDefault();
}
});
我怎样才能禁用一个锚点link 如果我的六 (6) 个复选框中有一个 (1) 未选中?
var first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094');
$("a").click(function(e) {
if($("first_option").prop("checked") === false) {
e.preventDefault(); return false;
} else {return true;};
});
first_option.prop("checked")
将始终检查第一个元素。您需要做的是遍历所有元素以检查
像这样
$("#tmp_button-99035").click(function(e) {
var isChecked = false;
for (var i = 0; i < first_option.length; i++) {
if (first_option.eq(i).prop("checked")) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert('Please Choose Collar Colour To Continue');
e.preventDefault();
}
return isChecked;
});
您当前的逻辑不起作用,因为您只查看第一个元素的 checked
属性 select,而不是所有元素。
要实现您的要求,您可以使用 :checked
selector 获取您提供的 select 中的所有选中元素,然后检查 length
属性 的结果,看看有没有。试试这个:
var $first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094');
$("#tmp_button-99035").click(function(e) {
if ($first_option.filter(':checked').length === 0) {
e.preventDefault();
alert('Please Choose Collar Colour To Continue');
};
});
好吧,你的 js 片段只检查第一个元素。因此,您还必须跟踪其他复选框以获得正确的结果。
var first_option = $('#pid-1590083, #pid-1590090, #pid-1590091, #pid-1590092, #pid-1590093, #pid-1590094')
$(document).on('click', '#tmp_button-99035', function (e) {
if ($(first_option).filter(":checked").length == 0) {
e.preventDefault();
}
});