简体   繁体   English

如何使用jquery显示选定的选项值?

[英]How to show the selected option value using jquery?

I was able to append values in an array to the select option. 我能够将数组中的值附加到选择选项。 When I do that I am not able to see the text value(A1) in the selection box. 当我这样做时,我无法在选择框中看到文本值(A1)。 But when I use an alert box it shows A1 as the selected option. 但是,当我使用警报框时,它显示A1作为所选选项。

My jQUERY CODE: 我的jQUERY代码:

var route=['A1','A2','A3','A4','A5','A6','A7',......,'A50']
$.each(route, function(key, value) {
 $('#room').append($('<option>', { value : key }).text(value)); 

if (!$("#room option:selected").length)
$("#room option[value='0']").attr('selected', 'selected'); 

This is my html code: 这是我的html代码:

<div data-role="fieldcontain"> 
<select name="room" id="room">  
</select>          
</div>

Can anyone help me on this one? 有人可以帮我吗?

Try this instead 试试这个

var route=['A1','A2','A3','A4','A5','A6','A7','A50'], html;
$.each(route, function(key, value) {
  html += '<option value="'+key+'">'+value+'</option>';
});
$('#room').append(html);

if ($("#room").val() !== '0') {
  $("#room").val(0); // using .val() will select the correct option for you, based on it's value attribute
} ​

see: http://jsfiddle.net/tyPFu/ 请参阅: http//jsfiddle.net/tyPFu/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM