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

时间:2021-07-12 00:11:08

My requirement is that if check-box is checked then you have to select something from the dropdown box. I have multiple rows in the gridview so i am only requiring that you have to make a selection from the dropdown if you select the checkbox.

我的要求是,如果选中复选框,则必须从下拉框中选择一些内容。我在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 个解决方案

#1


0  

i have solved so in case someone might have similar issue

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

<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>

#1


0  

i have solved so in case someone might have similar issue

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

<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>