我一生都无法弄清楚为什么 JQuery 在这段代码中找不到选择列表。我尝试过在几个 JQuery 测试页面中访问 Select 元素,它看起来总是很好,但这只是没有做任何事情:
<html>
<head>
<script src="jquery.js"></script>
<script>
$( document ).ready(function() {
var new_options = ['were filled with rocks', 'smelled like chicken pox', 'belonged to angry green crocs', 'are kept in a mouldy box' ];
/* Remove all options from the select list */
$( "#activity_1" ).empty();
/* Insert the new ones from the array above */
$.each(new_options, function(value) {
$( "#activity_1" ).append($('<option>', {value: 'Testing', text : 'Testing'}));
});
});
</script>
</head>
<body>
<select id='#activity_1'><option value='dud'>This shouldn't be here</option</select>,
</body>
</html>
最佳答案
改变
<select id='#activity_1'><
// ^
到
<select id='activity_1'><
您的选择器$('#activity')
搜索ID为“activity”而不是“#activity”的元素
关于javascript - JQuery 无法找到选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631983/