我试图将' selected
'添加到元素,但代码根本不执行。
有什么建议吗?
代码是:
$.post('./question_in_test.php',
{
questionId : vars[0]
} ,
function(data) {
$("#testQuestion > option").each(function () {
console.log("Something happens"); // Doesn't execute at all
if (this.value === data) {
$(this).prop('selected', 'selected');
}
});
console.log("NUmber id : " + data + " vars[0] : " + vars[0] ); // Shows on the console
}
);
在PHP
页上,我有一个从数据库检索的select
。
<select id = "testQuestion">
<optgroup label="Category 1">
<option>Option 1...</option>
</optgroup>
</select>
</select>
您的选项似乎在optgroup
标记中,因此选择没有直接的子选项。尝试以下选择器,减去子>
选择器:
$("#testQuestion option").each(function(){...});