2012-05-05 9 views
0

私のプログラムでは、一度に1つの質問を表示し、次にクリックすると投稿であり、2番目の質問が表示されます。しかし、それで問題が1つの質問のためのロジックは、他のチェックボックスリストの質問と私は異なっている、ということです私はjqueryの内の各質問のためのチェックボックスリストのためのいくつかのロジックを適用し、私はcheckBoxのchange clickで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); 
       } 
      } 
     }); 

    }); 

このようにそれを呼び出します上記のdocument.readyにあるように適切な関数を呼び出す必要がありますが、2つの関数を持っています。だから、私は関数呼び出しを修正する必要がありますいくつかの特定の質問はどうですか?

答えて

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

    }); 
}); 

は、それが

を役に立てば幸い
関連する問題