2012-03-20 54 views
1

我在其中一個web應用程序中有一個checkboxlist控件。我想用JavaScript來處理SelectedIndexChanged事件。使用javascript獲取SelectedIndexChanged複選框列表事件

複選框列表就像

<asp:CheckBoxList ID="CheckBoxList1" runat="server"> 
    <asp:ListItem>One</asp:ListItem> 
    <asp:ListItem>Two</asp:ListItem> 
    <asp:ListItem>Three</asp:ListItem> 
    <asp:ListItem>Four</asp:ListItem> 
    <asp:ListItem>Five</asp:ListItem> 
</asp:CheckBoxList> 

我怎樣才能使用javascript SelectedIndexChanged事件?

回答

3

在服務器端..把follwoing ..

CheckBoxList1.Attributes.Add("onclick", "ChangeCheckBox();"); 

在客戶端的JavaScript部分,實現以下功能

function ChangeCheckBox() {} 
+0

謝謝期待你的答覆。 – MithunRaj 2012-03-20 03:59:13

+0

是的,我可以用這個獲得複選框列表事件。 此外,我的用戶界面中有一個複選框,所以我使用相同的onclick來獲取複選框事件。但是當選中複選框時,我沒有收到此事件。我們是否應該爲複選框使用不同的事件? – MithunRaj 2012-03-22 02:25:18

0

您可以使用下面的代碼

document.getElementById('CheckBoxList1').onchange = function() { 
       var input = document.getElementById('CheckBoxList1').getElementsByTagName("input") 
       for (var i = 0; i < input.length; i++) { 
        if (input[i].type == "checkbox") { 
         if (input[i].checked == true) { 
          alert(input[i].value);//Get all the checked checkboxes 
         } 
        } 
       } 
      } 
相關問題