如果使用javascript选中复选框,则验证下拉框

时间:2016-01-27 01:23:37

标签: javascript jquery asp.net gridview

我的要求是,如果选中复选框,则必须从下拉框中选择一些内容。我在gridview中有多行,因此如果您选中该复选框,我只需要从下拉列表中进行选择。

    function validateDDL() {
      var flag = true;
      var dropdowns = new Array(); //Create array to hold all the dropdown lists.
      var gridview = document.getElementById('<%=gvSearch.ClientID%>'); //GridView1 is the id of ur gridview.
      dropdowns = gridview.getElementsByTagName('Select'); //Get all dropdown lists contained in GridView1.
      for (var i = 0; i < dropdowns.length; i++) {
        if (dropdowns.item(i).value == 'Select') //If dropdown has no selected value
        {
          flag = false;
          break; //break the loop as there is no need to check further.
        }
      }
      if (!flag) {
        dropdowns[i].focus();
        alert('Please select a Project Role from the dropdown box.  Thanks');

      }
      return flag;
    }
  </script> 

gridview的

 <asp:GridView ID="myGridview" runat="server"
      AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
      AllowPaging="true" ShowFooter="True"
      OnPageIndexChanging="OnPaging">
      <Columns>
        <asp:TemplateField HeaderText="ID" Visible="true">
          <ItemTemplate>
            <asp:Label ID="lblID" runat="server" Text='<%# Eval("PRJ_ID")%>'></asp:Label>
          </ItemTemplate>
          <ItemStyle Width="10px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title" Visible="true">
          <ItemTemplate>
            <asp:Label ID="lblPrjTit" runat="server" Text='<%# Eval("PRJ_TITLE")%>'></asp:Label>
          </ItemTemplate>
          <%-- <ItemStyle Width="10px" />--%>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Project Role">
          <ItemTemplate>
            <asp:DropDownList ID="ddlRole" CssClass="form-control" runat="server">
            </asp:DropDownList>
          </ItemTemplate>

        </asp:TemplateField>
        <asp:TemplateField HeaderText="Check">
          <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" />
          </ItemTemplate>
          <ItemStyle Width="10px" />
        </asp:TemplateField>

      </Columns>
    </asp:GridView>

1 个答案:

答案 0 :(得分:0)

我已经解决了,以防有人可能有类似的问题

<script type="text/javascript">
      function validateDDL() {
          //debugger;

      var flag = true;
      var gridView = document.getElementById('<%= gvSearch.ClientID %>');

      for (var i = 1; i < gridView.rows.length; i++) {
        var inputs = gridView.rows[i].getElementsByTagName('input');
        var dropdowns = gridView.rows[i].getElementsByTagName('Select');

       if (inputs != null && inputs.length >= 1 && inputs[0] != null && dropdowns != null && dropdowns.length >= 1 && dropdowns[0] != null) {

            if (dropdowns[0].type == "select-one" && inputs[0].type == "checkbox") {
              var ddlSelectedItem = dropdowns[0].value;

             // if (inputs[0].checked && (ddlSelectedItem == "Select" || txtval == null)) {
                if (inputs[0].checked && ddlSelectedItem == "Select" ) {

                flag = false;
                break;
              }
              else {
                flag = true;

              }
            }
          }
        }

      if (!flag) {
        alert('your message alert here');

    }
    return flag;
  }

  </script>