2012-05-05 36 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"); 
     } 

    }); 
}); 

希望它有助于