vb.net 使用javascript设置下拉列表选定的索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13462383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:13:23  来源:igfitidea点击:

Set Dropdownlist selected index with javascript?

javascriptasp.netvb.net

提问by user1732364

I have 2 dropdownlists on a asp page.

我在一个 asp 页面上有 2 个下拉列表。

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

如果用户更改了第一个下拉列表的选定索引,则设置 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..

并执行相同的逻辑,除了分别切换 DDL1 和 DDL2。我从相同的对象列表中填充了这些(只是为每个对象设置了不同的属性),并且我在查询上有一个 order by 子句以确保数据保持同步。我的问题是如何让这个逻辑在 javascript 中工作?我目前的方法是这样的..

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!

这段代码不起作用,但展示了我拍摄的目的。当获得第一个选择的 ddl 失去焦点时,填充另一个 ddl(设置所选索引)。任何帮助都会很棒!



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 compiles and just doesn't work... :( These drop downs are inside of a asp gridview.

它可以编译但不起作用... :( 这些下拉菜单位于 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.

问题是我从不使用 document.ready,因为下拉列表在 gridview 中。我实际上只是在学习 javascript/jquery,因为我遇到了这样的问题,所以请随意破解知识鞭子大声笑。

采纳答案by user1732364

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

我终于想通了!!!!jquery优先的解决方案如下

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

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