checkboxlist用JS获取不到value值的解决方法

using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

using  System.Collections.Generic;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        List
< string [] >  metadata  =   new  List < string [] > ();

        metadata.Add(
new   string [] {  " 项目前期 " " 505004 " });
        metadata.Add(
new   string [] {  " 项目进行中 " " 505001 "  });
        metadata.Add(
new   string [] {  " 项目中期 " " 505003 "  });
        metadata.Add(
new   string [] {  " 项目后期 " " 505005 "  });
        metadata.Add(
new   string [] {  " 项目结束 " " 505002 "  });
        metadata.Add(
new   string [] {  " 项目意外中止 " " 505006 "  });


        ListItem lt;

        
for  ( int  i  =   0 ; i  <  metadata.Count; i ++ )
        {
            lt 
=   new  ListItem(metadata[i][ 0 ], metadata[i][ 1 ]);
            lt.Attributes[
" text " =  metadata[i][ 0 ];
            lt.Attributes[
" value1 " =  metadata[i][ 1 ];
            
this .CheckBoxList1.Items.Add(lt);
        }
    }
}





HTML code
<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
    
< script language = " javascript "  type = " text/javascript " >
    function checkAll()
    {
        var ckelems 
=  document.getElementById( " CheckBoxList1 " ).getElementsByTagName( " input " );
        var spelems 
=  document.getElementById( " CheckBoxList1 " ).getElementsByTagName( " span " );
        
for (var i = 0 ;i < ckelems.length;i ++ )
        {
            
if (ckelems[i].type == " checkbox " )
            {
                
if (ckelems[i]. checked )
                    alert(
' 文本是  ' + spelems[i].text + '  值是  ' + spelems[i].value1);   
            }
        } 
    }
    
</ script >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:CheckBoxList ID = " CheckBoxList1 "  runat = " server "  Height = " 261px "  Width = " 301px " >
        
</ asp:CheckBoxList >
        
< input id = " Button1 "  type = " button "  value = " button "   onclick = " checkAll() " /></ div >
    
</ form >
</ body >
</ html >

你可能感兴趣的:(checkbox)