Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery脚本在不应该运行函数时运行函数';不可能_Jquery_Jquery Selectors - Fatal编程技术网

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脚本,除了在不应该的时候触发警报之外,一切正常,我不知道为什么

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 &gt;&gt; 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);
});