2014-03-03 47 views
0

在我的Javascript文件中,我有兩個不同複選框的監聽器,但是當我檢查/取消選中它們時,它們沒有被調用。下面是複選框的HTML:複選框未被調用的監聽器

<input type="checkbox" class="checkbox" name="ApproverCheckBox" id="@Approver[0]" checked/>@Approver[1]</li> 
<input type="checkbox" class="checkbox" name="AccessorCheckBox" id="@entry[0]" checked/>@entry[1]</li> 

這裏的JavaScript文件未執行部分:

$(document).ready(function() { 

    //listener for accessor checkbox 
    $('input[name=ApproverCheckBox]').change(function() { 

     if ($(this).is(':checked')) { 
      //AddAccessor(this.id); 
      alert("it is working"); 
     } 
     else { 
      //RemoveAccessor(this.id); 
      alert("it is working"); 
     } 

    }); 

    //listener for approver checkbox 
    $('input[name=ApproverCheckBox]').change(function() { 

     if ($(this).is(':checked')) { 
      //AddApprover(this.id); 
      alert("it is working"); 
     } 
     else { 
      //RemoveApprover(this.id); 
      alert("it is working"); 
     } 

    }); 

}); 

我添加的警報他們同時測試...可能有人提供一些線索請點亮。 (添加HTML後地方)

//listener for accessor checkbox 
    $(document).on('change','input[name=ApproverCheckBox]', function() { 

     if ($(this).is(':checked')) { 
      //AddAccessor(this.id); 
      alert("it is working"); 
     } 
     else { 
      //RemoveAccessor(this.id); 
      alert("it is working"); 
     } 

    }); 

    //listener for approver checkbox 
    $(document).on('change','input[name=ApproverCheckBox]', function() { 

     if ($(this).is(':checked')) { 
      //AddApprover(this.id); 
      alert("it is working"); 
     } 
     else { 
      //RemoveApprover(this.id); 
      alert("it is working"); 
     } 

    }); 
+0

其工作http://jsfiddle.net/fJkpH/1/ –

+0

該腳本在js小提琴中工作。是動態創建的HTML嗎? – JelleP

+0

我確認它正在工作。你是否正確導入了jQuery庫? –

回答

0

試試這個。

+0

我剛剛嘗試這個功能,我只需添加您已有的功能並返回你 – user2405469

+0

不幸的是這個解決方案沒有工作,它不執行聽衆。 – user2405469

+0

您可以嘗試在創建元素後立即創建偵聽器。 – JelleP

0

文檔準備從未被因爲文件準備是有裏面的錯誤錯誤地添加腳本調用: