vb.net 使用jQuery同时设置两个下拉列表的选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19177852/
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 15:20:48  来源:igfitidea点击:

Set the selected value of two dropdownlists simultaneously using jQuery

javascriptjqueryasp.netvb.netjquery-mobile

提问by DreamTeK

How to set the selected value of a dropdownlist to the selected value of another?

如何将下拉列表的选定值设置为另一个的选定值?

The below code shows how to accomplish this correctly.

下面的代码显示了如何正确完成此操作。

ASPX

ASPX

<asp:DropDownList ID="ddl1" RunAt="Server" DataValueField="ID" DataTextField="ID" AppendDataBoundItems="true"/>
<asp:DropDownList ID="ddl2" RunAt="Server" DataValueField="ID" DataTextField="TEXT" AppendDataBoundItems="true"/>

JQUERY

查询

$("#<%= ddl1.ClientID %>").change(function() {
    var Code = $(this).val();
    $("#<%= ddl2.ClientID %>").val(Code);
});

When using jQuery Mobile, however the DataTextField is rendered as a span tag which the above code does not update.

但是,当使用jQuery Mobile 时,DataTextField 被呈现为上面的代码不会更新的 span 标记。

QUESTION

How to amend the above code to accommodate for this?

如何修改上面的代码以适应这种情况?

http://jsfiddle.net/E28WW/

http://jsfiddle.net/E28WW/

采纳答案by Omar

Store the value from first selectmenu, then look for it in the second one. Select it using .prop('selected', true);and then call .selectmenu('refresh').

存储第一个选择菜单中的值,然后在第二个选择菜单中查找它。使用选择它 .prop('selected', true);,然后调用.selectmenu('refresh')

Demo

演示

$(document).on('pageinit', function () {
  $(document).on('change', '#ddl1', function () {
    var selected = $(this).val();
    $('#ddl2 option[value="' + selected + '"]').prop('selected', true);
    $('#ddl2').selectmenu('refresh');
  });
});

I used pageinitwhich is equivalent to .ready(). It is a special event for jQM.

我使用pageinit它相当于.ready(). 这是 jQM 的一个特殊事件。

回答by Nunners

Have you checked that the changefunction is actually being called. Is the script you provide the entire contents of the script tag?

您是否检查过该change函数实际上正在被调用。您提供的脚本是否提供了脚本标记的全部内容?

The function will not be bound to the event until it is loaded, using the $(document).ready();will bind the event to the control.

函数在加载之前不会绑定到事件,使用$(document).ready();将事件绑定到控件。

$(document).ready(function() {
    $("#<%= ddl1.ClientID %>").change(function() {
        var Code = $(this).val();
        $("#<%= ddl2.ClientID %>").val(Code);
    });
});