我正在尝试将“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>
答案 0 :(得分:2)
您的选项似乎位于optgroup
标记中,因此select没有直接子选项。尝试使用以下选择器,减去子>
选择器:
$("#testQuestion option").each(function(){...});