listbox select issue
I have the code provided below. I've tried using jquery to select to make something hap开发者_如何转开发pen but eventually what I have doesnt work or may be incorrect.
$("#emailList option").click(function() {
alert("OMG");
});
<select id="emailList" multiple="multiple" name="emailList">
<option>abc@123.com</option>
</select>
can someone provide me with the correct way of selecting an item from my listbox?
Try:
$("#emailList").change(function() {
alert($('option:selected', $(this)).text());
});
You can use the .change()
method like this:
$("#emailList").change(function() {
alert("Current value:" + $(this).val());
});
Since your <option>
has no value, the text will be the value, so using .val()
works here. The .click()
event doesn't execute on all browsers (IE...) for the <option>
elements, so it's better to use .change()
.
精彩评论