开发者

How to select all values in drop down list by clicking a button using jQuery?

开发者 https://www.devze.com 2023-01-15 16:35 出处:网络
If I use this $(\"select 开发者_如何学Pythonoption\").attr(\"selected\", \"true\"); Or $(function(){

If I use this

$("select 开发者_如何学Pythonoption").attr("selected", "true");

Or

$(function(){
    $('select').children('option').attr('selected', 'selected');
});

the list of dropdown will be selected automatically.

But in my position I should not use Iterator. Here both function using iterator. Here each and every option we set true or selected through iterator.

I need like if I set true to object of list, all the value of list should be selected without using loop or iterator.


The difference between selected="true" and selected="selected" is that one is for HTML and the other is for XHTML.

In HTML you use selected="true" (You used to just use selected, with no value, but using selected="true" is more explicit).

$("select option").attr("selected", "true");

In XHTML you use selected="selected"

$("select option").attr("selected", "selected");


If you want to do anything with a list of things, you iterate or enumerate through them (whichever the situation calls for), this is how programming works. Just use the selectors the way they were intended, like this:

$(function() {
  $('select option').attr('selected', true);
});
0

精彩评论

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