2012-05-05 33 views
0

在我的程序中,我一次顯示一個問題,然後點擊它發佈並獲得第二個問題。但在我申請的複選框列表中的一些邏輯jQuery的每一個問題,我這樣稱呼它如何唯一標識checkBox上的jQuery函數更改點擊?

$(document).ready(function() { 


     $("input:checkbox").change(function() { 
      var txt = $(this).parent().children("label").text(); 
      if ($(this).is(':checked')) { 
       if ($(this).parent().children("label").text() == 'Not Sure') { 
        $("input:checkbox").attr('checked', false); 
        $("input:checkbox").attr('disabled', true); 
        $(this).attr('checked', true); 
        $(this).attr('disabled', false); 
       } 
      } 
      else { 
       if ($(this).parent().children("label").text() == 'Not Sure') { 
        $("input:checkbox").attr('checked', true); 
        $("input:checkbox").attr('disabled', false); 
        $(this).attr('checked', false); 

       } 
      } 
     }); 


     $("input:checkbox").change(function() { 
      var txt = $(this).parent().children("label").text(); 
      if ($(this).is(':checked')) { 
       if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') { 
        $("input:checkbox").attr('checked', false); 
        $("input:checkbox").attr('disabled', true); 
        $(this).attr('checked', true); 
        $(this).attr('disabled', false); 
       } 
      } 
      else { 
       if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') { 
        $("input:checkbox").attr('checked', false); 
        $("input:checkbox").attr('disabled', false); 
        $(this).attr('checked', false); 
       } 
      } 
     }); 

    }); 

所以問題是,對於1個問題的邏輯是從其他複選框列表的問題和我不同需要調用相應的函數,如上面你在document.ready中看到的那樣有兩個函數。那麼如何解決一些特定的問題,我應該修復調用函數?

回答

0

在每個帖子中使用jQuery unbind函數來移除事件處理程序綁定。供參考的 查看this

0

你可以使用一些自定義的事件,並根據問題的,這裏的一些代碼觸發相應的事件:

$(document).ready(function() { 
    $("input:checkbox").on("someQuestions", function() { 
     var txt = $(this).parent().children("label").text(); 
     if ($(this).is(':checked')) { 
      if ($(this).parent().children("label").text() == 'Not Sure') { 
       $("input:checkbox").attr('checked', false); 
       $("input:checkbox").attr('disabled', true); 
       $(this).attr('checked', true); 
       $(this).attr('disabled', false); 
      } 
     } 
     else { 
      if ($(this).parent().children("label").text() == 'Not Sure') { 
       $("input:checkbox").attr('checked', true); 
       $("input:checkbox").attr('disabled', false); 
       $(this).attr('checked', false); 
      } 
     } 
    }); 

    $("input:checkbox").on("anotherQuestions", function() { 
     var txt = $(this).parent().children("label").text(); 
     if ($(this).is(':checked')) { 
      if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') { 
       $("input:checkbox").attr('checked', false); 
       $("input:checkbox").attr('disabled', true); 
       $(this).attr('checked', true); 
       $(this).attr('disabled', false); 
      } 
     } 
     else { 
      if (txt == 'Not Sure' || txt == 'We Are Large Business' || txt == 'None of these apply') { 
       $("input:checkbox").attr('checked', false); 
       $("input:checkbox").attr('disabled', false); 
       $(this).attr('checked', false); 
      } 
     } 
    }); 

    $("input:checkbox").change(function(){ 

     // apply some logic to find out if is one question or another 
     if (someQuestion) { 
      $(this).trigger("someQuestions"); 
     } 
     else { 
      $(this).trigger("anotherQuestions"); 
     } 

    }); 
}); 

希望它有助於