如何使用 jQuery 设置 DropDownList 的值?

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

How can I set the value of a DropDownList using jQuery?

jquerydrop-down-menu

提问by flesh

As the question says, how do I set the value of a DropDownList control using jQuery?

正如问题所说,如何使用 jQuery 设置 DropDownList 控件的值?

回答by Nick Berardi

$("#mydropdownlist").val("thevalue");

just make sure the value in the options tags matches the value in the val method.

只需确保选项标签中的值与 val 方法中的值相匹配。

回答by mattsson

If you working with index you can set the selected index directly with .attr():

如果您使用索引,则可以直接使用 .attr() 设置所选索引:

$("#mydropdownlist").attr('selectedIndex', 0);
$("#mydropdownlist").attr('selectedIndex', 0);

This will set it to the first value in the droplist.

这会将其设置为下拉列表中的第一个值。

Edit:The way I did it above used to work. But it seems like it doesn't any longer.

编辑:我上面做的方式曾经奏效。但是现在好像没有了。

But as Han so pleasantly points out in the comments, the correct way to do it is:

但正如 Han 在评论中如此愉快地指出的那样,正确的做法是:

$("#mydropdownlist").get(0).selectedIndex = index_here;

回答by IRSHAD

As suggested by @Nick Berardi, if your changed value is not reflected on the UI front end, try:

正如@Nick Berardi 所建议的,如果您更改的值未反映在 UI 前端,请尝试:

$("#mydropdownlist").val("thevalue").change();

回答by Clyde

Try this very simple approach:

试试这个非常简单的方法:

/*make sure that value is included in the options value of the dropdownlist 
e.g. 
(<select><option value='CA'>California</option><option value='AK'>Alaska</option>      </select>)
*/

$('#mycontrolId').val(myvalue).attr("selected", "selected");

回答by salah9

If your dropdown is Asp.Net drop down then below code will work fine,

如果您的下拉菜单是 Asp.Net 下拉菜单,那么下面的代码可以正常工作,

 $("#<%=DropDownName.ClientID%>")[0].selectedIndex=0;

But if your DropDown is HTML drop down then this code will work.

但是,如果您的 DropDown 是 HTML 下拉菜单,则此代码将起作用。

 $("#DropDownName")[0].selectedIndex=0;

回答by Asif Ramzan

Best answer for the question:

问题的最佳答案:

$("#comboboxid").val(yourvalue).trigger("chosen:updated");

e.g:

例如:

$("#CityID").val(20).trigger("chosen:updated");

or

或者

$("#CityID").val("chicago").trigger("chosen:updated");

回答by Jignesh Joisar

set value and updatedropdown list event

设置值和更新下拉列表事件

$("#id").val("1234").change();

回答by Dilip Kumar Yadav

There are many ways to do it. here are some of them:

有很多方法可以做到。这里是其中的一些:

$("._statusDDL").val('2');

OR

或者

$('select').prop('selectedIndex', 3);

回答by JapineJ

I think this may help:

我认为这可能会有所帮助:

    $.getJSON('<%= Url.Action("GetDepartment") %>', 
              { coDepartment: paramDepartment },
              function(data) {
                    $(".autoCompleteDepartment").empty();
                    $(".autoCompleteDepartment").append($("<option />").val(-1));
                    $.each(data, function() {
                        $(".autoCompleteDepartment").append($("<option />").val(this.CodDepartment).text(this.DepartmentName));
                    });
                    $(".autoCompleteDepartment").val(-1);                                       
              }  
             );

If you do this way, you add an element with no text. So, when you click de combo, it doesn't apear, but the value -1 you added a later select with $(".autoCompleteDepartment").val(-1); let you control if the combo has a valid value.

如果您这样做,您将添加一个没有文本的元素。因此,当您单击 de combo 时,它不会出现,但是您稍后添加的值 -1 选择了 $(".autoCompleteDepartment").val(-1); 让您控制组合是否具有有效值。

Hope it helps anybody.

希望它可以帮助任何人。

Sorry for my english.

对不起我的英语不好。

回答by Mannu saraswat

If you just need to set the value of a dropdown then the best way is

如果您只需要设置下拉列表的值,那么最好的方法是

$("#ID").val("2");

If You have to trigger any script after updating the dropdown value like If you set any onChange event on the dropdown and you want to run this after update the dropdown than You need to use like this

如果您必须在更新下拉值后触发任何脚本,例如如果您在下拉列表上设置任何 onChange 事件,并且您想在更新下拉列表后运行它,那么您需要像这样使用

$("#ID").val("2").change();