jQuery脚本在不应该运行函数时运行函数';不可能
我已经破解了这个jQuery脚本,除了在不应该的时候触发警报之外,一切正常,我不知道为什么jQuery脚本在不应该运行函数时运行函数';不可能,jquery,jquery-selectors,Jquery,Jquery Selectors,我已经破解了这个jQuery脚本,除了在不应该的时候触发警报之外,一切正常,我不知道为什么 jQuery(document).ready(function(){ function countDouble() { var d = $("input.double:checked").length; var s = $("input.single:checked").length; if (s === 1 && d === 2
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 x.单复选框和2 x.双复选框时,它会将#tab2的URL添加到按钮中,这样可以正常工作。
但选中复选框时,单击按钮后,它也会显示警报3次。仅当未选中复选框时,才会发出警报。
我做错了什么?我无法解决此问题请尝试在每次调用
countDouble()
时取消绑定“click”,并将countDouble()
拉到ready函数之外(可能没有必要)
请参阅此表单,以便在函数外部添加事件处理程序:
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);
});
为什么要为
$(“#proj btn”)添加事件处理程序。在该函数中单击(
)?-每次调用它时,它都会再次被添加。这很管用,Michael。当然,现在看起来很明显:)干杯
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);
});
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);
});