开发者

Jquery access to selectbox text not value how?

开发者 https://www.devze.com 2022-12-08 07:54 出处:网络
example: <select> <option value=\'1\'>hello me<option> <option value=\'2\'>hello world</option>

example:

<select>
<option value='1'>hello me<option>
<option value='2'>hello world</option>
</select>

how can I access a TEXT not VALUE from select box

so I can display HELLO ME or HELLO WORLD inst开发者_JAVA技巧ead of 1 & 2.


Use text together with the :selected form selector:

$('select option:selected').text()

Loop over all options' texts and values:

$('#mySelect option').each(function () {
    console.log(this.text + ' ' + this.value);
});

Grab all option texts into an array:

var textArr = $('#mySelect option').map(function (i,n) {
    return $(n).text();
}).get();
console.log(textArr.join(','));


If you want to access the text of the selected option you might use:

$("#list option:selected").text();

Alternatively you could access the option directly this way:

$("#list option[value=2]").text();

#list is the id of the specific select. You should assign an id (e. g. <select id="list">) in order to avoid conflicts with other select tags.


var text = $(option[value='1']).text();
0

精彩评论

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

关注公众号