我已经将这个jQuery脚本整合在一起了,除非警报在不应该被触发时被解雇,否则一切正常。我无法解决原因。
jQuery(document).ready(function(){
function countDouble() {
var d = $("input.double:checked").length;
var s = $("input.single:checked").length;
if (s === 1 && d === 2) {
$("a#proj-btn").attr("href", "#tab2");
} else {
$("a#proj-btn").attr("href", "http://www.someurl.com.au/new-cruise-name-home");
$("#proj-btn").click( function() {
alert('Please select which projects you are interested in before continuing with enquiry.\nSelect 1 x 1 day project and 2 x 2 day projects by ticking the box beneath your chosen projects.');
});
};
}
countDouble();
$(":checkbox").click(countDouble);
});
这是HTML
<ul class="tabs">
<li class="right"><a id="proj-btn" href="#">NEXT >> Cabin Choice</a></li>
</ul>
因此,如果选中了1个.single复选框并选中了2个.double复选框,则会将#tab2的URL添加到按钮中,该工具正常。 但是,当选中复选框时,它还会在单击按钮后显示警报3次。警报应仅在未选中复选框时。 我做错了什么?我无法解决这个问题
答案 0 :(得分:1)
每次拨打countDouble()
并将countDouble()
拉到就绪功能之外时,请尝试取消绑定“点击”(可能不需要)。
function countDouble() {
var d = $("input.double:checked").length;
var s = $("input.single:checked").length;
//Add This:
$("#proj-btn").unbind('click');
if (s === 1 && d === 2) {
$("a#proj-btn").attr("href", "#tab2");
} else {
$("a#proj-btn").attr("href", "http://www.someurl.com.au/new-cruise-name-home");
$("#proj-btn").click( function() {
alert('Please select which projects you are interested in before continuing with enquiry.\nSelect 1 x 1 day project and 2 x 2 day projects by ticking the box beneath your chosen projects.');
});
};
}
jQuery(document).ready(function(){
countDouble();
$(":checkbox").click(countDouble);
});
答案 1 :(得分:0)
jQuery(document).ready(function(){
function countDouble() {
var d = $("input.double:checked").length;
var s = $("input.single:checked").length;
if (s === 1 && d === 2) {
$("#proj-btn").unbind("click");//changed
$("a#proj-btn").attr("href", "#tab2");
} else {
$("a#proj-btn").attr("href", "http://www.someurl.com.au/new-cruise-name-home");
$("#proj-btn").click( function() {
alert('Please select which projects you are interested in before continuing with enquiry.\nSelect 1 x 1 day project and 2 x 2 day projects by ticking the box beneath your chosen projects.');
});
};
}
countDouble();
$(":checkbox").click(countDouble);
});
答案 2 :(得分:0)
请参阅此表单,将您的事件处理程序添加到函数外部:
jQuery(document).ready(function () {
$("#proj-btn").click(function () {
alert('Please select which projects you are interested in before continuing with enquiry.\nSelect 1 x 1 day project and 2 x 2 day projects by ticking the box beneath your chosen projects.');
});
function countDouble() {
var d = $("input.double:checked").length;
var s = $("input.single:checked").length;
if (s === 1 && d === 2) {
$("a#proj-btn").attr("href", "#tab2");
} else {
$("a#proj-btn").attr("href", "http://www.someurl.com.au/new-cruise-name-home");
}
}
countDouble();
$(":checkbox").click(countDouble);
});