javascript - jQuery - select item just clicked - Stack Overflow

I would like to know which item in select list was last clickedI have select drop down like this<sel

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745590005a4634775.html

相关推荐

  • javascript - jQuery - select item just clicked - Stack Overflow

    I would like to know which item in select list was last clickedI have select drop down like this<sel

    11小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信