开发者

How do you select items in a listbox using jQuery?

开发者 https://www.devze.com 2022-12-25 20:31 出处:网络
How do you programmatically select items in a multi-select listbox开发者_开发知识库 using jQuery?You can do it like this:

How do you programmatically select items in a multi-select listbox开发者_开发知识库 using jQuery?


You can do it like this:

var valToSelect = "1";
$("#mySelect option[value='" + valToSelect + "']").attr("selected", "true");

Here's a quick example: http://jsfiddle.net/ZyAHr/

Just for kicks, here's an alternative example if it fits the situation:

var values = $("select").val();
values.push("1");
$("select").val(values);

​Here's a quick example of this: http://jsfiddle.net/FBRFY/

This second approach takes advantage of the fact that .val() on a multiple <select> element returns an array, not a string. You can get it, add or remove any values, then set it again using .val() and it'll be updated with the new selection.


In ListBox that have multi selection mode use it :

  $('#ListBox1').find('option:selected').map(function () {
  alert($(this).text());
  });


var hasListItems = $('#ddlListItems option:selected').toArray().map(item => item.text).join();
0

精彩评论

暂无评论...
验证码 换一张
取 消