开发者

jQuery getting text value for a select list

开发者 https://www.devze.com 2022-12-21 17:55 出处:网络
I have a list like this: <select name=\"select_list_name\" id=\"list_id\"> <option value=\"\">Select Option</option>

I have a list like this:

<select name="select_list_name" id="list_id">
    <option value="">Select Option</option>
    <option value="value1">Option 1</option>
    <option value="value2">Option 2</option>
    ...
    ...开发者_运维百科
</select>

I am trying to get the text value of the currently selected option in a select list. I looked at this thread: jQuery get specific option tag text and tried this:

$("#list_id option:selected").text()

But this only gets me the first options text ("Select Option") regardless of which option has been selected.

I tried another way:

$("[name=select_list_name] option:selected").text()

That gets me the first option's text concatenated with the selected options's text ("Select OptionOption 2" if I select Option 2).

Any idea on why?


$('#list_id :selected').text(); should give you the selected option's text.

Something else in your code must be wrong -- this piece of code really works


This WORKS, 100%, do you have more than one id with 'list_id'?

$('#list_id :selected').text();
0

精彩评论

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