ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Javascript得到CheckBoxList的Value

Javascript得到CheckBoxList的Value

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-10-27 16:37:56 0 删除 编辑
主要思路就是为ListItem加一个"checkValue"属性,前台通过javascript找到。
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckBoxListValue.aspx.cs" Inherits="test_CheckBoxListValue" %>

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>Javascript得到CheckBoxList的Valuetitle>
    
<script type="text/javascript" src="../js/jquery-1.3.2.min.js">script>
    
<script type="text/javascript">
        
//把选中的值写入hiddenfield
        function f(){
            
var total = $("#chkBox input[@type=checkbox]:checked").size();
            
var checkValues = '';
            $(
"#chkBox input[@type=checkbox]:checked").each(function(){
                checkValues 
+= $(this).parent().attr("checkValue") + ",";
            });
            alert(
"选中了" + total + "个" + "\n" + "值:" + checkValues.substring(0,checkValues.length-1));
        }
    
script>
head>
<body>
    
<form id="form1" runat="server">
    
<div>
        主要思路就是为ListItem加一个"checkValue"属性,前台通过javascript找到。
    
div>
    
<div>
        
<input type="button" onclick="f();" value="取值" />
        
<asp:CheckBoxList ID="chkBox" runat="server">
        
asp:CheckBoxList>
    
div>
    
form>
body>
html>

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class test_CheckBoxListValue : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
            Bind();
    }
    
private void Bind()
    {
        ListItem li1 
= new ListItem("1", "1");
        li1.Attributes.Add(
"checkValue", "1");
        chkBox.Items.Add(li1);

        ListItem li2 
= new ListItem("2", "2");
        li2.Attributes.Add(
"checkValue", "2");
        chkBox.Items.Add(li2);
    }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-617527/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766066