0

我已经破解了这个 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.Single复选框时,检查2 x .double复选框时,它会将#tab2的URL添加到正常工作的按钮。但是,当复选框被选中时,它也会在单击按钮后显示 3 次警报。仅当未选中复选框时才应发出警报。我做错了什么?我无法解决这个问题

4

3 回答 3

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);  
});
于 2013-01-23T22:55:27.720 回答
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);  
}); 
于 2013-01-23T22:59:31.433 回答
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);
});
于 2013-01-23T23:01:23.200 回答