有沒有辦法使用javascript或mootools更改html選擇列表的選項數組?更改選擇列表的選項數組
我需要用新的設置替換整個選項集。在我的Ajax響應我收到填滿了新的HTML選項的數組,所以我儘量清空舊列表和添加新值如下
$('element').options.length=0;
for (i=0; i<newSet.length; i++)
{
$('element').options[i]=newSet[i];
}
上面的代碼給我一個未捕獲的異常就行內部循環。
未捕獲的異常:[異常... 「意外錯誤」 nsresult: 「0x8000ffff(NS_ERROR_UNEXPECTED)」 的位置:「JS框架
只是爲了增加對我工作:
/* get new options from json*/
var new_options = response.options;
/* Remove all options from the select list */
$('idresource').empty();
/* Insert the new ones from the array above */
for (var key in new_options)
{
var opt = document.createElement('option');
opt.text = new_options[key];
opt.value = key;
$('idresource').add(opt, null);
}
有一個錯字在'$每個(new_options ...'。它應該是'$ .each',而不是'$ each'。 – Kevin 2013-10-11 23:50:09
@Kevin - 這是針對Mootools的,而不是jQuery,所以它是'$ each'。 – 2013-10-12 13:16:31