none
js 如何判断 RadioButtonList 有无选择值: RRS feed

  • 问题

  • 下面的JS
     function check()
        {
          var opts = document.all("RadioButtonList1");
             if (opts)
             {
                 for (var i = 0; i < opts.length; i++)
                 {
                     if (opts[i].checked)
                     {
                         alert(i);
                         break;
                     }
                     else
                     {
                     alert ('没有选择任何项')
                 }

          }
        }

      上面是当我没有选择任何值的情况下,有多少项就弹出多少次  ('没有选择任何项')  ;如何只弹出一次呢?急。。。。


    小吴
    • 已移动 Sheng Jiang 蒋晟 2009年5月16日 15:20 客户端脚本问题 ([Loc]From:ASP.NET 与 AJAX)
    2009年5月16日 2:59

答案

  • 你那样是不行的
    以下是两种方法,检查全部和单行检查2种方法

    <%@ Page Language="C#" AutoEventWireup="true" Debug="true" EnableEventValidation="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
      public string stArr = "var rArr = [];";
      protected void Page_Load(object sender, EventArgs e)
      {
        GridView1.DataSource = this.CreateDataSourceByXianhuiMeng();
        GridView1.DataBind();
      }
      System.Data.DataView CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("StudentID", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
    
        for (int i = 0; i < 20; i++)
        {
          System.Random rd = new System.Random(Environment.TickCount * i); ;
          dr = dt.NewRow();
          dr[0] = i.ToString();
          dr[1] = "【孟子E章】" + i.ToString();
          dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
          dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
          dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
          dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
          dt.Rows.Add(dr);
        }
        System.Data.DataView dv = new System.Data.DataView(dt);
        return dv;
      }
    
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
          RadioButtonList r = e.Row.FindControl("RadioButtonList1") as RadioButtonList;
          stArr += "rArr[" + e.Row.RowIndex.ToString() + "] = '" + r.UniqueID + "';";
    
          Button b = e.Row.FindControl("Button1") as Button;
          b.OnClientClick = "check(" + e.Row.RowIndex.ToString() + ");return false;";
        }
      }
    
      protected void GridView1_Load(object sender, EventArgs e)
      {
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "j", stArr, true);
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <script type="text/javascript">
        function check(m) {    
            var thisRowChecked = false;
            var r = document.getElementsByName(rArr[m])
            for (j = 0; j < r.length; j++) {
              if (r[j].checked) {
                thisRowChecked = true
              }
            }
            if (!thisRowChecked) {
              alert("第 " + m + " 行的radio没有选择");
            } 
          }
    
        function checkAll() {
          for (i = 0; i < rArr.length; i++) {
            var thisRowChecked = false;
            var r = document.getElementsByName(rArr[i])
            for (j = 0; j < r.length; j++) {
              if (r[j].checked) {
                thisRowChecked = true
              }
            }
            if (!thisRowChecked) {
              alert("第 " + i + " 行的radio没有选择");
            }
          }
        }
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onrowdatabound="GridView1_RowDataBound" onload="GridView1_Load">
        <Columns>
          <asp:BoundField DataField="学生姓名" HeaderText="bh" InsertVisible="False" ReadOnly="True"
            SortExpression="学生姓名" />
          <asp:TemplateField>
            <ItemTemplate>
              <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                <asp:ListItem Value="0">ffffffff</asp:ListItem>
                <asp:ListItem Value="1">tttttttttt</asp:ListItem>
              </asp:RadioButtonList>
              <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
      <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="checkAll();return false;"  />
      </form>
    </body>
    </html>
    

    【孟子E章】
    2009年5月16日 10:09
    版主

全部回复

  • function check()
    {
        var opts = document.all("RadioButtonList1");
        if (opts)
        {
            var fIsSelected = false;
            for (var i = 0; i < opts.length; i++)
            {
                if (opts[i].checked)
                {
                    fIsSelected = true;
                    alert(i);
                    break;
                }
            }
            if (fIsSelected == false)
            {
                alert('没有选择任何项')
            }
        }
    }


    知识改变命运,奋斗成就人生!
    2009年5月16日 3:05
    版主
  • x.x.y 谢谢你啦!..... 厉害,
    小吴
    2009年5月16日 3:18
  •  

    function check()
    
    {
    
        var opts = document.all("RadioButtonList1");
    
        if (opts)
    
        {
    
            var fIsSelected = false;
    
            for (var i = 0; i < opts.length; i++)
    
            {
    
                if (opts[i].checked)
    
                {
    
                    fIsSelected = true;
    
                    alert(i);
    
                    break;
    
                }
    
            }
    
            if (fIsSelected == false)
    
            {
    
                alert('没有选择任何项')
    
            }
    
        }
    
    }
    我在里面用   JS 识别不到:    代码如下:帮我看看中吧:
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="bh" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="bh" HeaderText="bh" InsertVisible="False" ReadOnly="True"
                    SortExpression="bh" />
                    <asp:TemplateField>
                    <ItemTemplate>
                      <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                      <asp:ListItem Value="0">ffffffff</asp:ListItem>
                      <asp:ListItem Value="1">tttttttttt</asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="check()" />
                    </ItemTemplate>
                    </asp:TemplateField>
            </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:wlsqConnectionString %>"
                SelectCommand="SELECT [bh] FROM [news_xwwj]"></asp:SqlDataSource>




     


    知识改变命运,奋斗成就人生!


    小吴
    2009年5月16日 3:37
  • 你那样是不行的
    以下是两种方法,检查全部和单行检查2种方法

    <%@ Page Language="C#" AutoEventWireup="true" Debug="true" EnableEventValidation="true" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
      public string stArr = "var rArr = [];";
      protected void Page_Load(object sender, EventArgs e)
      {
        GridView1.DataSource = this.CreateDataSourceByXianhuiMeng();
        GridView1.DataBind();
      }
      System.Data.DataView CreateDataSourceByXianhuiMeng()
      {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("StudentID", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
    
        for (int i = 0; i < 20; i++)
        {
          System.Random rd = new System.Random(Environment.TickCount * i); ;
          dr = dt.NewRow();
          dr[0] = i.ToString();
          dr[1] = "【孟子E章】" + i.ToString();
          dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
          dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
          dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
          dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
          dt.Rows.Add(dr);
        }
        System.Data.DataView dv = new System.Data.DataView(dt);
        return dv;
      }
    
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
          RadioButtonList r = e.Row.FindControl("RadioButtonList1") as RadioButtonList;
          stArr += "rArr[" + e.Row.RowIndex.ToString() + "] = '" + r.UniqueID + "';";
    
          Button b = e.Row.FindControl("Button1") as Button;
          b.OnClientClick = "check(" + e.Row.RowIndex.ToString() + ");return false;";
        }
      }
    
      protected void GridView1_Load(object sender, EventArgs e)
      {
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "j", stArr, true);
      }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
      <script type="text/javascript">
        function check(m) {    
            var thisRowChecked = false;
            var r = document.getElementsByName(rArr[m])
            for (j = 0; j < r.length; j++) {
              if (r[j].checked) {
                thisRowChecked = true
              }
            }
            if (!thisRowChecked) {
              alert("第 " + m + " 行的radio没有选择");
            } 
          }
    
        function checkAll() {
          for (i = 0; i < rArr.length; i++) {
            var thisRowChecked = false;
            var r = document.getElementsByName(rArr[i])
            for (j = 0; j < r.length; j++) {
              if (r[j].checked) {
                thisRowChecked = true
              }
            }
            if (!thisRowChecked) {
              alert("第 " + i + " 行的radio没有选择");
            }
          }
        }
      </script>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onrowdatabound="GridView1_RowDataBound" onload="GridView1_Load">
        <Columns>
          <asp:BoundField DataField="学生姓名" HeaderText="bh" InsertVisible="False" ReadOnly="True"
            SortExpression="学生姓名" />
          <asp:TemplateField>
            <ItemTemplate>
              <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                <asp:ListItem Value="0">ffffffff</asp:ListItem>
                <asp:ListItem Value="1">tttttttttt</asp:ListItem>
              </asp:RadioButtonList>
              <asp:Button ID="Button1" runat="server" Text="Button" />
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>
      <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="checkAll();return false;"  />
      </form>
    </body>
    </html>
    

    【孟子E章】
    2009年5月16日 10:09
    版主
  • 谢谢你!孟老师........


    小吴
    2009年5月16日 12:13