c# (javascript)操作checkbox控制項

來源:互聯網
上載者:User
<%--author wangaihui--%><%--date 2008-8-10--%><%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    //伺服器端全部選中或取消    protected void Button1_Click(object sender, EventArgs e)    {        foreach (Control c in this.form1.Controls)        {            if (c is CheckBox)            {                ((CheckBox)c).Checked = !((CheckBox)c).Checked;            }        }        if (this.Button1.Text == "全部選中")        {            this.Button1.Text = "取消全選";        }        else        {            this.Button1.Text = "全部選中";        }    }    //伺服器端得到全部選中的名字    protected void Button2_Click(object sender, EventArgs e)    {        int count = 0;        foreach (Control c in this.form1.Controls)        {            if (c is CheckBox)            {                if ((c as CheckBox).Checked)                {                    count++;                    this.TextBox1.Text += c.ID.ToString() + "; ";                }            }        }        this.TextBox1.Width = count * 100;     }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>無標題頁</title>    <script language="javascript" type="text/javascript">    //用戶端全部選中或取消    function mySelect()    {    //如果用戶端不支援javascript則交到伺服器端處理      if(!document.getElementById)      {      return true;      }      var inputList=document.getElementsByTagName('input');      for(var i=0;i<inputList.length;i++)      {        if(inputList[i].type=="checkbox")        {          inputList[i].checked=!inputList[i].checked;        }      }      var button1= document.getElementById("Button1");      if(button1.value=='全部選中')      {        button1.value='全部取消';      }      else      {        button1.value='全部選中';      }            return false;    }    //用戶端得到全部選中的名字    function mySelectCheckBoxName()    {    //如果用戶端不支援javascript則交到伺服器端處理      if(!document.getElementById)      {        return true;      }      var inputList=document.getElementsByTagName('input');      var namelist='';      var count=0;      for(var i=0;i<inputList.length;i++)      {        if(inputList[i].type=="checkbox")        {          if(inputList[i].checked)          {            namelist+=inputList[i].name+"; ";            count++;          }        }      }           var textBox1= document.getElementById("TextBox1");           textBox1.value=namelist;     //有可能textBox1的長度不夠長所以根據選中的個數讓它自動成長     textBox1.setAttribute('width',100*count);              return false;    }        </script></head><body>    <form id="form1" runat="server">    <div>        <div>            <table>                <tr>                    <td colspan="6">                        please select your love:</td>                </tr>                <tr>                    <td style="width: 100px">                        dancing</td>                    <td style="width: 100px">                        <asp:CheckBox ID="CheckBox1" runat="server" Text=" " /></td>                    <td style="width: 100px">                        song</td>                    <td style="width: 100px">                        <asp:CheckBox ID="CheckBox2" runat="server" Text=" " /></td>                    <td style="width: 100px">                        sport</td>                    <td style="width: 100px">                        <asp:CheckBox ID="CheckBox3" runat="server" Text=" " /></td>                </tr>                <tr>                    <td style="width: 100px">                        tour</td>                    <td style="width: 100px">                        <asp:CheckBox ID="CheckBox4" runat="server" Text=" " /></td>                    <td style="width: 100px">                        music</td>                    <td style="width: 100px">                        <asp:CheckBox ID="CheckBox5" runat="server" Text=" " /></td>                    <td style="width: 100px">                        party</td>                    <td style="width: 100px">                        <asp:CheckBox ID="CheckBox6" runat="server" Text=" " /></td>                </tr>            </table>            <br />            <br />            <asp:Button ID="Button1" runat="server" OnClientClick="return mySelect();" OnClick="Button1_Click" Text="全部選中" />            <br />            <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>            <br />            <asp:Button ID="Button2" OnClientClick="return mySelectCheckBoxName();" runat="server" Text="選中的CheckBox的名子" OnClick="Button2_Click" /><br />        </div>        </div>    </form></body></html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.