最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set the SELECTED property of dropdown in jQuery? - Stack Overflow

programmeradmin5浏览0评论

I am sending the index number of dropdown's options that users selects from index. Now want to select that specific option as 'selected' on another view. How to make it using jQuery?

$('#book_selection').attr("disabled", "disabled");
$('#book_selection').selectedIndex = 1;

but its not working...

I am sending the index number of dropdown's options that users selects from index. Now want to select that specific option as 'selected' on another view. How to make it using jQuery?

$('#book_selection').attr("disabled", "disabled");
$('#book_selection').selectedIndex = 1;

but its not working...

Share Improve this question edited Apr 6, 2014 at 1:53 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Jan 1, 2014 at 11:04 Baqer NaqviBaqer Naqvi 6,5223 gold badges54 silver badges71 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Use prop() method, setting selectedIndex to a jQuery object practically does nothing. As of jQuery 1.6 for modifying properties prop method should be used instead of attr method:

$('#book_selection').prop("disabled", true)
                    .prop('selectedIndex', 1);

Alternatives:

// Getting the DOM element object using bracket notation and `.get()` method
$('#book_selection')[0].selectedIndex = 1;
$('#book_selection').get(0).selectedIndex = 1;

Use .prop() for setting the properties, By the way you are in need to set the value by using index, so in that case .eq(index) will help you.

Try,

$('#book_selection option').eq(1).prop('selected',true);
发布评论

评论列表(0)

  1. 暂无评论