'How to get and Set Gridview Dropdownlist Selectedvalue In JavaScript Or JQuery

I have a gridview in which i have two Templatefields which are having dropdownlist as ItemTemplates. On First DropDownList's SelectedValue Changed event,i want to get Second DropDownList's Value & check it with the firt's and vice versa. Both DropDownList's SelectedValue should not be the same.

Gridview is as follows :

<asp:GridView ID="grdFinalSite" runat="server" AutoGenerateColumns="false" Width="95%"
                    CssClass="mGrid" DataKeyNames="EmpId">
                    <Columns>
                        <asp:BoundField DataField="EmpCode" HeaderText="Employee Code" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="EmpFullName" HeaderText="Employee Name" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:TemplateField HeaderText="Primary Site" ItemStyle-Width="120px">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlPrimary" runat="server" CssClass="ddlPrim" Width="110px">
                                </asp:DropDownList>
                                <asp:Label ID="lblMandatoryResult" runat="server" Text="*" ForeColor="Red" Visible="true"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Secondary Site" ItemStyle-Width="120px">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlSecondary" runat="server" CssClass="ddlSec" Width="110px">
                                </asp:DropDownList>
                                <asp:Label ID="lblMandatory" runat="server" Text="*" ForeColor="Red" Visible="true"></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

I am using following JQuery code.But for the second dropdownlist,it gives selectedvalue as 'undefined'.

$(document).ready(function () {
        $(".ddlSec").change(function () {
            if ($(this).val() != "0") {
                var Primary = $(this).val();
                var value = $(this).closest("tr").find("input[type=select][id*=ddlPrimary]").value;

                alert(Primary + " " + value);
            }
        });
    });

Please help me to solve this problem.

Thanks in advance.



Solution 1:[1]

Below format worked for me.

var ddlList = $("[id*=gridviewdropdownlistID]"); var selectedtext = ddlList.find("option:selected").text();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vasudeva N S