admin管理员组

文章数量:1022804

I have 2 dropdownlists on a asp page.

If user changes the selected index of the first drop down list, then set DDL2.selectedindex = DDL1.Selectedindex

and do this same logic except switch DDL1 and DDL2 respectively. I have these both getting populated from the same list of objects(just different properties set to each) and i have a order by clause on the query to ensure the data stays in sync. My question is how can i get this logic to work in javascript? My current method is as such..

Accounts.Attributes.Add("onBlur", Customers.SelectedIndex = Accounts.SelectedIndex)
Customers.Attributes.Add("onBlur", Accounts.SelectedIndex = Customers.SelectedIndex)

This code doesn't work but demonstrates what im shooting for. When the ddl getting the first selection loses focus, populate the other ddl(setting the selected index). Any help would be great!


Can someone see what i'm doing wrong here?

            $("[id$=ddlStandardAcctNo]").change(function () {

            var acc = $("[id$=ddlStandardAcctNo]");
            var cust = $("[id$=ddlCustomerName]");
            cust.selectedindex = acc.selectedindex;
        });

It piles and just doesn't work... :( These drop downs are inside of a asp gridview.


After looking at that i'm trying to do this..

        $("[id$=ddlStandardAcctNo]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        cust.selectedindex = acc.selectedindex
    });

    $("[id$=ddlCustomerName]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        acc.selectedindex = cust.selectedindex
    });

Problem is i never use document.ready cause the dropdownlist are in a gridview. I'm literally just learning javascript/jquery as i run across issues like this so feel free to crack the knowledge whip lol.

I have 2 dropdownlists on a asp page.

If user changes the selected index of the first drop down list, then set DDL2.selectedindex = DDL1.Selectedindex

and do this same logic except switch DDL1 and DDL2 respectively. I have these both getting populated from the same list of objects(just different properties set to each) and i have a order by clause on the query to ensure the data stays in sync. My question is how can i get this logic to work in javascript? My current method is as such..

Accounts.Attributes.Add("onBlur", Customers.SelectedIndex = Accounts.SelectedIndex)
Customers.Attributes.Add("onBlur", Accounts.SelectedIndex = Customers.SelectedIndex)

This code doesn't work but demonstrates what im shooting for. When the ddl getting the first selection loses focus, populate the other ddl(setting the selected index). Any help would be great!


Can someone see what i'm doing wrong here?

            $("[id$=ddlStandardAcctNo]").change(function () {

            var acc = $("[id$=ddlStandardAcctNo]");
            var cust = $("[id$=ddlCustomerName]");
            cust.selectedindex = acc.selectedindex;
        });

It piles and just doesn't work... :( These drop downs are inside of a asp gridview.


After looking at that i'm trying to do this..

        $("[id$=ddlStandardAcctNo]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        cust.selectedindex = acc.selectedindex
    });

    $("[id$=ddlCustomerName]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        acc.selectedindex = cust.selectedindex
    });

Problem is i never use document.ready cause the dropdownlist are in a gridview. I'm literally just learning javascript/jquery as i run across issues like this so feel free to crack the knowledge whip lol.

Share Improve this question edited May 3, 2013 at 16:31 user1228 asked Nov 19, 2012 at 21:15 user1732364user1732364 9934 gold badges28 silver badges60 bronze badges 3
  • 4 Calm down, @gdoron. It's not a big deal. This isn't Nazi Germany. – Yatrix Commented Nov 19, 2012 at 21:21
  • @Yatrix, not a teenagers chat as well... – gdoron Commented Nov 19, 2012 at 21:22
  • 4 @gdoron Well, at least you're not taking the site too seriously or anything. Thank you for saving SO from this monster. Before you get chased away with pitchforks and torches user, you should be able to find most of what you're looking for here: stackoverflow./questions/6210390/… – Yatrix Commented Nov 19, 2012 at 21:29
Add a ment  | 

1 Answer 1

Reset to default 1

I figured this out finally!!!! the solution for jquery prior is the following

                        $("[id$=ddlStandardAcctNo]").change(function () {
                        $("[id$=ddlCustomerName]").attr("selectedIndex", this.selectedIndex);
                    });

                    $("[id$=ddlCustomerName]").change(function () {
                        $("[id$=ddlStandardAcctNo]").attr("selectedIndex", this.selectedIndex);
                    });

I have 2 dropdownlists on a asp page.

If user changes the selected index of the first drop down list, then set DDL2.selectedindex = DDL1.Selectedindex

and do this same logic except switch DDL1 and DDL2 respectively. I have these both getting populated from the same list of objects(just different properties set to each) and i have a order by clause on the query to ensure the data stays in sync. My question is how can i get this logic to work in javascript? My current method is as such..

Accounts.Attributes.Add("onBlur", Customers.SelectedIndex = Accounts.SelectedIndex)
Customers.Attributes.Add("onBlur", Accounts.SelectedIndex = Customers.SelectedIndex)

This code doesn't work but demonstrates what im shooting for. When the ddl getting the first selection loses focus, populate the other ddl(setting the selected index). Any help would be great!


Can someone see what i'm doing wrong here?

            $("[id$=ddlStandardAcctNo]").change(function () {

            var acc = $("[id$=ddlStandardAcctNo]");
            var cust = $("[id$=ddlCustomerName]");
            cust.selectedindex = acc.selectedindex;
        });

It piles and just doesn't work... :( These drop downs are inside of a asp gridview.


After looking at that i'm trying to do this..

        $("[id$=ddlStandardAcctNo]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        cust.selectedindex = acc.selectedindex
    });

    $("[id$=ddlCustomerName]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        acc.selectedindex = cust.selectedindex
    });

Problem is i never use document.ready cause the dropdownlist are in a gridview. I'm literally just learning javascript/jquery as i run across issues like this so feel free to crack the knowledge whip lol.

I have 2 dropdownlists on a asp page.

If user changes the selected index of the first drop down list, then set DDL2.selectedindex = DDL1.Selectedindex

and do this same logic except switch DDL1 and DDL2 respectively. I have these both getting populated from the same list of objects(just different properties set to each) and i have a order by clause on the query to ensure the data stays in sync. My question is how can i get this logic to work in javascript? My current method is as such..

Accounts.Attributes.Add("onBlur", Customers.SelectedIndex = Accounts.SelectedIndex)
Customers.Attributes.Add("onBlur", Accounts.SelectedIndex = Customers.SelectedIndex)

This code doesn't work but demonstrates what im shooting for. When the ddl getting the first selection loses focus, populate the other ddl(setting the selected index). Any help would be great!


Can someone see what i'm doing wrong here?

            $("[id$=ddlStandardAcctNo]").change(function () {

            var acc = $("[id$=ddlStandardAcctNo]");
            var cust = $("[id$=ddlCustomerName]");
            cust.selectedindex = acc.selectedindex;
        });

It piles and just doesn't work... :( These drop downs are inside of a asp gridview.


After looking at that i'm trying to do this..

        $("[id$=ddlStandardAcctNo]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        cust.selectedindex = acc.selectedindex
    });

    $("[id$=ddlCustomerName]").blur(function () {

        var acc = document.getElementById('<%=ddlStandardAcctNo.ClientID %>');
        var cust = document.getElementById('<%=ddlCustomerName.ClientID %>');
        acc.selectedindex = cust.selectedindex
    });

Problem is i never use document.ready cause the dropdownlist are in a gridview. I'm literally just learning javascript/jquery as i run across issues like this so feel free to crack the knowledge whip lol.

Share Improve this question edited May 3, 2013 at 16:31 user1228 asked Nov 19, 2012 at 21:15 user1732364user1732364 9934 gold badges28 silver badges60 bronze badges 3
  • 4 Calm down, @gdoron. It's not a big deal. This isn't Nazi Germany. – Yatrix Commented Nov 19, 2012 at 21:21
  • @Yatrix, not a teenagers chat as well... – gdoron Commented Nov 19, 2012 at 21:22
  • 4 @gdoron Well, at least you're not taking the site too seriously or anything. Thank you for saving SO from this monster. Before you get chased away with pitchforks and torches user, you should be able to find most of what you're looking for here: stackoverflow./questions/6210390/… – Yatrix Commented Nov 19, 2012 at 21:29
Add a ment  | 

1 Answer 1

Reset to default 1

I figured this out finally!!!! the solution for jquery prior is the following

                        $("[id$=ddlStandardAcctNo]").change(function () {
                        $("[id$=ddlCustomerName]").attr("selectedIndex", this.selectedIndex);
                    });

                    $("[id$=ddlCustomerName]").change(function () {
                        $("[id$=ddlStandardAcctNo]").attr("selectedIndex", this.selectedIndex);
                    });

本文标签: aspnetSet Dropdownlist selected index with javascriptStack Overflow