2013-01-23 58 views
0

jQuery腳本一起砍了這個jQuery腳本,它都工作正常,除了警報被解僱時,它不應該是,我不能解決原因。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個。單選框選中和2×。雙複選框選中,它增加了#TAB2的URL到工作正常的按鈕。 但是,在複選框被選中後,點擊按鈕後也會顯示警告3次。該警報只應在未選中複選框的情況下進行。 我做錯了什麼?我無法解決這個問題

+0

你爲什麼要添加的事件處理程序'$( 「#PROJ-BTN」)單擊( 「那個函數裏面 - 每一次被調用,它都會被重新添加。」 –

回答

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); 
}); 
+0

這個效果很好,當然現在看起來很明顯:)乾杯 – user537137

+0

好..很高興幫助! –

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