簡體   English   中英

在多個gridview行中使用jQuery將值從一個文本框復制到另一個文本框

[英]Copy value from one textbox to another with jQuery in multiple gridview rows

我有一個網格視圖,用戶可以一次添加1行或更多行。 如果結束日期文本框為空,則需要將開始日期復制到結束日期文本框。 如果在添加行時網格為空,則可以使用此方法,但是稍后在向網格中添加行時,我不知道如何執行此操作。

這是因為我可能只有兩行帶有文本框,所以它們的數組只有2個條目,但是我的行索引可能表明這是第六行。 我不知道如何確定正在使用哪個文本框,因此可以將其復制到同一行的結束日期文本框。

網格視圖

<asp:GridView ID="gvOfferingDates" runat="server" 
    AutoGenerateColumns="False" 
    onrowdatabound="gvOfferingDates_RowDataBound" 
    onrowediting="gvOfferingDates_RowEditing" 
    onrowcancelingedit="gvOfferingDates_RowCancelingEdit" 
    onrowupdated="gvOfferingDates_RowUpdated" 
    onrowupdating="gvOfferingDates_RowUpdating" Width="100%" >
    <Columns>
        <asp:TemplateField>
            <HeaderStyle HorizontalAlign="Left" />
            <ItemTemplate>
                <asp:ImageButton ID="btnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/Edit.png" Visible='<%# !IsEditable(Eval("NewRow")) %>' />
                <asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png" Visible='<%# IsEditable(Eval("NewRow")) %>' />
                <asp:ImageButton ID="btnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/undo.png" CausesValidation="False" Visible='<%# EditMode %>' />--%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Begin Date">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblBeginDate" Text='<%# Eval("BeginDate", "{0:MM/dd/yyyy}") %>' Visible='<%# !IsEditable(Eval("NewRow")) %>'></asp:Label>
                <asp:Label runat="server" ID="lblBeginDateEdit" Text='<%# Eval("BeginDate", "{0:MM/dd/yyyy}") %>' Visible="false"></asp:Label>
                <asp:Label ID="lblBeginRequired" runat="server" Text="*" ForeColor="Red" Visible='<%# IsEditable(Eval("NewRow")) %>'></asp:Label>&nbsp;
                <asp:TextBox CssClass="begindate" ID="txtBeginDate" runat="server" Visible='<%# IsEditable(Eval("NewRow")) %>' BackColor="#FFFACD"></asp:TextBox>
            </ItemTemplate>

        </asp:TemplateField>
        <asp:TemplateField HeaderText="End Date">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblEndDate" Text='<%# Eval("EndDate", "{0:MM/dd/yyyy}") %>' Visible='<%# !IsEditable(Eval("NewRow")) %>'></asp:Label>
                <asp:Label runat="server" ID="lblEndDateEdit" Text='<%# Eval("EndDate", "{0:MM/dd/yyyy}") %>' Visible="false"></asp:Label>
                <asp:Label ID="lblEndRequired" runat="server" Text="*" ForeColor="Red" Visible='<%# IsEditable(Eval("NewRow")) %>'></asp:Label>&nbsp;
                <asp:TextBox CssClass="enddate" ID="txtEndDate" runat="server" BackColor="#FFFACD" CausesValidation="True" Visible='<%# IsEditable(Eval("NewRow")) %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

jQuery的

$(document).ready(function() {
    $(".begindate").datepicker({
        onSelect: function(dateText, ui) {
            var bDate = $(this).val();
            var iRowIndex = $(this).closest("tr").prevAll("tr").length;
            var $arrT = $('#<%=gvOfferingDates.ClientID %>').find('input:text[id$="txtEndDate"]');
            var txtEnd = $($arrT[iRowIndex - 1]);


            if ($($txtEnd).val().length == 0) {
                $($txtEnd).val(bDate);
            }
        },
        changeMonth: true, changeYear: true, minDate: new Date()
    }).on("change", function() {
        $(this).blur()
    });

    $(".enddate").datepicker({
        onSelect: function() {
            minDate: new Date($(".begindate"))
        },
        changeMonth: true, changeYear: true, minDate: new Date($(".begindate"))
    });

更新

將jquery更改為以下更簡單的方法適用於新記錄並添加到現有記錄。

$(".begindate").datepicker({
    onSelect: function(dateText, ui) {
        var bDate = $(this).val();

        var txtEnd = $(this).closest("tr").find('input:text[id$="txtEndDate"]');
        if (txtEnd.val().length == 0) {
            txtEnd.val(bDate);
        }

    },
    changeMonth: true, changeYear: true, minDate: new Date()
}).on("change", function() {
    $(this).blur()
});

如果您具有begindate元素,則應該可以通過以下步驟訪問enddate元素:

  • 獲取最接近的TR父元素。
  • 在TR子元素中搜索具有enddate id的輸入。 兩個文本框應位於同一表行中。

我不確定,但是會是這樣的:

 var txtEnd = $(begindateElement).closest("tr").find('input:text[id$="txtEndDate"]');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM