选择使用$ .post实时选择的选项

时间:2013-06-17 12:55:17

标签: php select post

我正在尝试将“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>

1 个答案:

答案 0 :(得分:2)

您的选项似乎位于optgroup标记中,因此select没有直接子选项。尝试使用以下选择器,减去子>选择器:

$("#testQuestion option").each(function(){...});