我有点被这段代码卡住了。我正在尝试检索选项文本。
HTML code
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
for(i=0; i<list.length; i++) { // assume list[i] contains different names
var name_options = $('<option>' + list[i] + '</option>');
$('#names').append(name_options);
}
/* I want to retrieve data when a particular option is selected. But this doesnt work*/
$('#names').click(function(){
var val = $(this).find(':selected').html();
});
});
</script>
</head>
<body>
<select id='names' class='combobox'>
!-- options for names go here -->
</select>
</body>
</html>
我无法检索所选的值。因此,$("#names")
由于$("#names option")
无法正常工作,因此我将其用作选择器。我在这里做错了什么?
您想使用change()
处理程序,就可以直接从<select>
自身获取值。
$('#names').change(function(){
var val = $(this).val();
});
在选择过程中,您将有几次单击事件,这是click()
帮助不大的原因之一
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句