admin管理员组文章数量:1431949
I would like to know which item in select list was last clicked
I have select drop down like this
<select id="selectId" multiple="multiple" onchange="">
<option value=1>Value 1</option>
<option value=2>Value 2</option>
<option value=3>Value 3</option>
<option value=4>Value 4</option>
</select>
I would like to know, which item from select was last clicked and if it is now selected or not. Which jQuery selector () should be used in this case?
I would like to know which item in select list was last clicked
I have select drop down like this
<select id="selectId" multiple="multiple" onchange="">
<option value=1>Value 1</option>
<option value=2>Value 2</option>
<option value=3>Value 3</option>
<option value=4>Value 4</option>
</select>
I would like to know, which item from select was last clicked and if it is now selected or not. Which jQuery selector (http://docs.jquery./Selectors) should be used in this case?
Share Improve this question asked Oct 15, 2009 at 11:22 AnzeRAnzeR 6004 gold badges11 silver badges24 bronze badges3 Answers
Reset to default 4You can use
$('#selectId option:selected');
to get the selected option
See
:selected
Wire an onclick event to select and store the clicked item. When a new click occurs pare the previous item with the new selected item.
Try using the click event on the <option>
element, this can tell you is the last option was selected or not (you can set this to a variable):
var lastOption;
$('option').click(function(){
lastOption = $(this);
var lastIsSelected = lastOption.is(':selected');
var lastText = lastOption.text();
// ...
});
See working code here: http://jsbin./ijowo
I would have a look at the selectBoxes plugin for Jquery (http://www.texotela.co.uk/code/jquery/select/)
It's very good for this sort of thing.
Example
jQuery('#selectId').selectedOptions().each( function () {
alert(this.text());
});
That would give you an alert with the text of each selected option. As stated above, you could monitor the selected options using the change event.
本文标签: javascriptjQueryselect item just clickedStack Overflow
版权声明:本文标题:javascript - jQuery - select item just clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745590004a2665141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论