如何扩展此脚本的功能?

时间:2017-12-08 00:40:40

标签: jquery asp.net

以下脚本允许用户使用名为 grid1Details 的复选框填写gridview控件中的文本框。

如果用户选择不填写文本框,则用户必须选中其他框中的 grid1Details 复选框以提交表单。

如果用户未在复选框上打勾或未在所有三个文本框中输入值,则用户会收到我们的自定义提醒消息 “请在所有文本框中输入值或选中下一个复选框到每个文本框。“

这很有效,这是脚本:

      <script type="text/javascript">
          function validate() {
              var checkbox = document.getElementById("<%=grid1Details.ClientID %>");
              var txtsourcename = $("[id*='txtsourcename']")[0];
              var txtsourceaddress = $("[id*='txtsourceaddress']")[0];
              var txtsourceincome = $("[id*='txtsourceincome']")[0];

              if (checkbox.checked) {
                  if ($(txtsourcename).val().length != 0 && $(txtsourceaddress).val().length != 0 && $(txtsourceincome).val().length != 0) {
                      jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                      return false;
                  } else {
                      return true;
                  }
              } else {
                  if ($(txtsourcename).val().length != 0 && $(txtsourceaddress).val().length != 0 && $(txtsourceincome).val().length != 0) {
                      return true;
                  } else {
                      jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                      return false;
                  }
              }
           }
   </script>

我们现在遇到的问题是我们有几个gridview控件,每个控件都有自己的复选框控件,必须以相同的方式验证。

在下一个脚本中,我们尝试添加 spouseDetails 复选框,但它无效。

每次,我们要么将数据输入两个复选框控件, grid1Details 控件和 spouseDetails 复选框控件并按下提交按钮,它不会验证< strong> spouseDetails 复选框。

验证 grid1Details 复选框,但忽略 spouseDetails 复选框。

我们做错了什么想法?

<script type="text/javascript">
    function validate() {
        var checkbox = document.getElementById("<%=grid1Details.ClientID %>");
        var txtsourcename = $("[id*='txtsourcename']")[0];
        var txtsourceaddress = $("[id*='txtsourceaddress']")[0];
        var txtsourceincome = $("[id*='txtsourceincome']")[0];

        //spouse check
        checkbox = document.getElementById("<%=spouseDetails.ClientID%>");
        var txtspousename = $("[id*='txtspousename']")[0];
        var txtspouseaddress = $("[id*='txtspouseaddress']")[0];
        var txtspouseincome = $("[id*='txtspouseincome']")[0];

        if (checkbox.checked) {
            if ($(txtsourcename).val().length != 0 && $(txtsourceaddress).val().length != 0 && $(txtsourceincome).val().length != 0) {
                jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                return false;
            } else {
                return true;
            }
            if ($(txtspousename).val().length != 0 && $(txtspouseaddress).val().length != 0 && $(txtspouseincome).val().length != 0) {
                jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                return false;
            } else {
                return true;
            }            
        } else {
            if ($(txtsourcename).val().length != 0 && $(txtsourceaddress).val().length != 0 && $(txtsourceincome).val().length != 0) {
                return true;
            } else {
                jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                return false;
            }
            if ($(txtspousename).val().length != 0 && $(txtspouseaddress).val().length != 0 && $(txtspouseincome).val().length != 0) {
                return true;
            } else {
                jAlert("Please enter values on all textboxes or check the checkbox next to each textbox.");
                return false;
            }            
        }
    }
   </script>


//click button:
      <td> <asp:Button ID="btnNext" CssClass="btnNext" runat="server" Text=" Review Form " OnClientClick="BtnClick(); return validate()" OnClick="btnNext_Click" /></td>


 <table>
    <tr>
        <td>
            <asp:CheckBox ID="grid1Details" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="Grid1CheckChanged" /><span style="color:#ff0000">*</span>
        </td>
        <td>
          <asp:gridview ID="Gridview1" gridlines="None" runat="server" ShowFooter="true" AutoGenerateColumns="false" onrowdatabound="Gridview1_RowDataBound" OnRowDeleting="Gridview1_RowDeleting">
            <Columns>
            <asp:BoundField DataField="RowNumber" Visible="false" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Name">
             <headerstyle horizontalalign="Left" />
                <ItemTemplate>
                    <asp:TextBox ID="txtsourcename" placeholder="Name..." runat="server" style="width:250px;" class="form-control"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address">
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                <ItemTemplate>
                    <asp:TextBox ID="txtsourceaddress" placeholder="Address..." runat="server" style="width:250px;" class="form-control"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Income">
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                <ItemTemplate>
                     <asp:TextBox ID="txtsourceincome" placeholder="Income...(example: 1000)" runat="server" style="width:250px;" class="form-control txtsourceincome numeric"></asp:TextBox>
                </ItemTemplate>
                       </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add" 
                        onclick="ButtonAdd_Click" CssClass="grvAddButton" OnClientClick="return ValidateEmptyValue();return validate()" />
                </ItemTemplate>
            </asp:TemplateField>
           <asp:CommandField ShowDeleteButton="True"><ControlStyle CssClass="grvDelButton" /></asp:CommandField>
            </Columns>
          </asp:gridview>
        </td>
    </tr>
</table>
<table>
    <tr>
        <td>
            <asp:CheckBox ID="spouseDetails" ClientIDMode="Static" runat="server" Checked="false" AutoPostBack="true" OnCheckedChanged="SpouseCheckChanged" /><span style="color:#ff0000">*</span>
        </td>
        <td>                     
            <asp:gridview ID="grvspouse" GridLines="None" runat="server" ShowFooter="true" AutoGenerateColumns="false" onrowdatabound="grvspouse_RowDataBound" OnRowDeleting="grvspouse_RowDeleting">
            <Columns>
            <asp:BoundField DataField="SpouseNumber" Visible="false" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Name">
            <headerstyle horizontalalign="Left" />
                <ItemTemplate>
                    <asp:TextBox ID="txtspousename" placeholder="Name..." runat="server" style="width:250px;" class="form-control"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address">
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                <ItemTemplate>
                    <asp:TextBox ID="txtspouseaddress" placeholder="Address..." runat="server" style="width:250px;" class="form-control"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Income">
            <ItemStyle HorizontalAlign="Left"></ItemStyle>
                <ItemTemplate>
                     <asp:TextBox ID="txtspouseincome" placeholder="Income...(example: 1000)" runat="server" style="width:250px;" class="form-control txtsourceincome numeric"></asp:TextBox>
                </ItemTemplate>
           </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                 <asp:Button ID="ButtonAdd2" runat="server" Text="Add" 
                        onclick="ButtonAdd2_Click" CssClass="grvAddButton" OnClientClick="return ValidateSPEmptyValue();" />
                </ItemTemplate>
            </asp:TemplateField>
           <asp:CommandField ShowDeleteButton="True"><ControlStyle CssClass="grvDelButton" /></asp:CommandField>
            </Columns>
          </asp:gridview>
        </td>
    </tr>
</table>
  <td> <asp:Button ID="btnNext" CssClass="btnNext" runat="server" Text=" Review Form " OnClientClick="BtnClick(); return validate()" OnClick="btnNext_Click" /></td>

0 个答案:

没有答案