[英]JQuery Not Able To Find Select List
我一生無法解決為什么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”的元素
您需要使用兩個反斜杠來使冒號轉義:
$( "#\\#activity_1" ).empty();
在下面也替換。
$.each(new_options, function(value) {
$( "#\\#activity_1" ).append($('<option>', {value: 'Testing', text : 'Testing'}));
});
編輯:
如果您希望使用任何元字符(如!“#$%&'()* +,。/ :; <=>?@ [] ^`{|}〜)作為a的原義部分,名稱,必須用兩個反斜杠轉義該字符:例如,如果您的元素具有id =“#foobar”,則可以使用選擇器$(“#\\#foobar”)W3C CSS規范包含完整的。
或者使用元素id
,使其不能包含任何元字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.