如何在嵌套网格视图中获取上一个和下一个兄弟元素的id

时间:2016-01-29 14:52:10

标签: jquery gridview nested

我有一个ASP.NET嵌套的gridview。插入行时,有2个下拉列表和一个文本框。第一个下拉列表的值设置第二个下拉列表的值。当我在第二个下拉列表中选择值时,将刷新值并包含所有可能的值,并且不受限制。在我的jquery change函数中,我需要确定上一个下拉列表的值。

我该怎么做?

这是我当前的功能,它将列出所有值,而不是option value限制它们。

$(document).on("change", "[id*=recdevgvDDListServiceNameInsert]", function () {
 if ($(this).is(":disabled") == false) {
    //This is the first problem...the value is returned as 0 because
    //it is not specifically identifies the previous sibling of the Service name DDL.
     var DeviceSelValue = $("[id*=recdevgvDDListDeviceNameInsert]").val();   
     alert('device value is: ' + DeviceSelValue);
     $(this).children("option").hide();
     $(this).trigger("chosen:updated");

     switch (DeviceSelValue) {
         case "1":
             $("[id*=recdevgvDDListServiceNameInsert] option[value*='cell']").show();
             $(this).trigger("chosen:updated");
             $("[id*=recdevgvAddressExtInsert]").show();
             break;
         case "2":
             $("[id*=recdevgvDDListServiceNameInsert] option[value*='email']").show();
             $(this).trigger("chosen:updated");
             $("[id*=recdevgvAddressExtInsert]").hide();
             break;
         case "3":
             $("[id*=recdevgvDDListServiceNameInsert] option[value*='page']").show();
             $(this).trigger("chosen:updated");
             $("[id*=recdevgvAddressExtInsert]").show();
             break;
         default:
             break;
     }
 }
});

我需要专门识别元素recdevgvDDListDeviceNameInsert的上一个兄弟recdevgvDDLServiceNameInsert

我尝试过使用: $(this).prev().attr('id');获取元素的id。但它返回' undefined'。 在嵌套网格视图中获取前一个和下一个元素的正确语法是什么?

感谢。

更新 根据要求,这是标记。它很大。

<asp:GridView ID="RecipientDeviceGridView" runat="server" AutoGenerateColumns="false" CssClass="grid" ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="DeviceID">
            <ItemTemplate>
                <asp:Label ID="recdevgvLblDeviceID" runat="server" Text='<%# Bind("DeviceID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Device"  ItemStyle-Wrap="false">
            <ItemTemplate>
                <asp:Label ID="recdevgvDeviceID" runat="server" Visible="false" Text='<%# Bind("DeviceID") %>'></asp:Label>
                <asp:Label ID="recdevgvLblDeviceName" runat="server" Text='<%# Bind("DeviceName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="recdevgvEditDeviceID" runat="server" Visible="false" Text='<%# Bind("DeviceID") %>'></asp:Label>
                <asp:Label ID="recdevgvEditDeviceName" runat="server" Visible="false" Text='<%# Bind("DeviceName") %>'></asp:Label>
                <asp:DropDownList ID="recdevgvDDListDeviceName" runat="server" ClientIDMode="Static" 
                    data-placeholder="Choose device…" class="chosen-single">
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="recdevReqValueDDLDeviceNameEdit" runat="server"  
                        ControlToValidate="recdevgvDDListDeviceName" ValidationGroup="recdevEditDeviceValidation" 
                        ErrorMessage="Selection required." CssClass="message-error-dropdown">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="recdevgvDDListDeviceNameInsert" runat="server" ClientIDMode="Predictable"
                    data-placeholder="Choose device..." class="chosen-single">
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="recdevReqValueDDLDeviceNameInsert" runat="server" InitialValue="0" 
                    ControlToValidate="recdevgvDDListDeviceNameInsert" ValidationGroup='<%# "recdevInsertDeviceValidation" + Eval("RecipientID") %>' 
                    ErrorMessage="Selection required." CssClass="message-error-dropdown">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Service Provider">
            <ItemTemplate>
                <asp:Label ID="recdevgvLblServiceName" runat="server" Text='<%# Bind("ServiceName") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="recdevgvEditServiceName" runat="server" Visible="false" Text='<%# Bind("ServiceName") %>'></asp:Label>
                <asp:DropDownList ID="recdevgvDDListServiceName" runat="server" ClientIDMode="Static" 
                    OnSelectedIndexChanged="RecipientDeviceGridView_SelectedIndexChanged_EditServiceName" AutoPostBack="true" EnableViewState="true"
                     data-placeholder="Choose service…" class="chosen-single">
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="recdevReqValueDDLServiceNameEdit" runat="server" 
                        ControlToValidate="recdevgvDDListServiceName" ValidationGroup="recdevEditDeviceValidation" 
                        ErrorMessage="Selection required." CssClass="message-error-dropdown">
                </asp:RequiredFieldValidator>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="recdevgvDDListServiceNameInsert" runat="server" Enabled="false" ClientIDMode="Predictable"
                    OnSelectedIndexChanged="RecipientDeviceGridView_SelectedIndexChanged_InsertServiceName" AutoPostBack="true" 
                    EnableViewState="true" data-placeholder="Choose service…" class="chosen-single">
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="recdevReqValueDDLServiceNameInsert" runat="server" InitialValue="0" 
                        ControlToValidate="recdevgvDDListServiceNameInsert" ValidationGroup='<%# "recdevInsertDeviceValidation" + Eval("RecipientID") %>'
                        ErrorMessage="Selection required." CssClass="message-error-dropdown">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Address">
            <ItemTemplate>
                <asp:Label ID="recdevgvLblAddress" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="recdevgvTxtBoxAddress" runat="server" Text='<%# Bind("Address") %>' ClientIDMode="Static"></asp:TextBox>
                <asp:Label ID="recdevgvEditAddressExt" runat="server" Visible="false" Text='<%# Bind("ServiceExtension") %>' 
                        ClientIDMode="Static">
                </asp:Label>
                <asp:RequiredFieldValidator ID="recdevReqValueAddressEdit" runat="server" 
                        ControlToValidate="recdevgvTxtBoxAddress" ValidationGroup="recdevEditDeviceValidation" 
                        ErrorMessage="Required field." CssClass="message-error">
                </asp:RequiredFieldValidator>
                <asp:CustomValidator ID="recdevCustomValAddressEdit" runat="server" ControlToValidate="recdevgvTxtBoxAddress" CssClass="message-error" 
                    ErrorMessage="*" ClientValidationFunction="ValidateAddressEdit" EnableClientScript="true" 
                    ValidationGroup="recdevEditDeviceValidation">
                </asp:CustomValidator>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="recdevgvTxtBoxAddressInsert" runat="server" ClientIDMode="Predictable"></asp:TextBox>
                <asp:Label ID="recdevgvAddressExtInsert" runat="server" Visible="false" ClientIDMode="Predictable"></asp:Label>
                <asp:RequiredFieldValidator ID="recdevReqValueAddressInsert" runat="server" 
                    ControlToValidate="recdevgvTxtBoxAddressInsert" ValidationGroup='<%# "recdevInsertDeviceValidation" + Eval("RecipientID") %>'
                    ErrorMessage="Required field." CssClass="message-error">
                </asp:RequiredFieldValidator>
                <asp:CustomValidator ID="recdevCustomValAddressInsert" runat="server" ControlToValidate="recdevgvTxtBoxAddressInsert" CssClass="message-error" 
                    ErrorMessage="*" ClientValidationFunction="ValidateAddressInsert" EnableClientScript="true" 
                    ValidationGroup='<%# "recdevInsertDeviceValidation" + Eval("RecipientID") %>'>
                </asp:CustomValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Active">
            <ItemTemplate>
                <asp:Label ID="recdevgvLblActive" runat="server" Text='<%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="recdevgvDDListActive" runat="server" class="no-chosen"
                    Text='<%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %>'>
                    <asp:ListItem>Yes</asp:ListItem>
                    <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="recdevgvDDListActiveInsert" runat="server" class="no-chosen">
                    <asp:ListItem Selected="True">Yes</asp:ListItem>
                    <asp:ListItem>No</asp:ListItem>
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>                                                 
        <asp:TemplateField HeaderText="Action" ShowHeader="False" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:Button ID="recdevgvEditButton" runat="server" CausesValidation="True" CommandName="Edit" 
                    Text="Edit" CssClass="gridActionbutton" ValidationGroup="EditDeviceValidation">
                </asp:Button>
                    &nbsp;<asp:Button ID="recdevgvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" 
                    Text="Delete" CssClass="gridActionbutton"  OnClientClick="return confirm('Are you sure you want to delete this Device Information?')" >
                </asp:Button>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="recdevgvUpdateButton" runat="server" CausesValidation="True" ValidationGroup="recdevEditDeviceValidation" CommandName="Update" 
                    Text="Update" CssClass="gridActionbutton"></asp:Button>
                    &nbsp;<asp:Button ID="recdevgvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" 
                    Text="Cancel" CssClass="gridActionbutton"></asp:Button>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:Button ID="recdevgvAddButton" runat="server" CommandName="AddDevice" Text="Add Device" CausesValidation="true" 
                    CssClass="gridActionbutton" ValidationGroup='<%# "recdevInsertDeviceValidation" + Eval("RecipientID") %>'>
                </asp:Button>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

更新 当我尝试使用以下方法识别前一个兄弟对象时:

$(document).on("change", "[id*=recdevgvDDListServiceNameInsert]", function () {
   if ($(this).is(":disabled") == false) {                  
      var deviceDDL = $(this).closest('td').prev('td').find('input'); 
      alert('device value is: ' + deviceDDL.text);
      $(this).children("option").hide();
      $(this).trigger("chosen:updated");
...rest of function is the same as above...

警告显示:

enter image description here

更新 当我得到元素html()的{​​{1}}时,我会得到值:

$(this).html()

我需要获得前一个兄弟,这也是一个下拉列表。 当我使用<option selected="selected" value="0"></option> <option value="1,cell">AirTouch</option> <option value="2,page">AirTouch Alpha Pager</option> <option value="3,cell">Ameritech</option> <option value="4,cell">Arch</option> <option value="5,cell">AT&amp;T Wireless</option> <option value="6,cell">Bell Atlantic Mobile</option> </select> 时,警报会显示“未定义”。

似乎不应该这么困难。函数$(this).prev().html()应该返回前一个兄弟元素,但它不会。

更新 使用prev()返回我需要所选值的下拉列表。当我尝试使用$(this).parent().prev().html()时,它会返回&#39; undefined&#39;。 我也尝试过:$(this).parent().prev().val()这也会返回undefined。

更新进度!! 我可以使用以下代码行获取设备的选定值: $(this).parent().prev('option:selected').val()

现在,我需要获取此对象的ID,我可以在行中替换var DeviceSelValue = $(this).parent().prev().find("option:selected").val();[id*=recdevgvDDListServiceNameInsert]

0 个答案:

没有答案