-1

我有一个动态填充选择框的 PHP 代码,例如:

for($i=0; $i<$n; $i++) {
  echo '<select id='s_.$i.'> <option> Yes </option> <option> No </option> </select>';
}

现在每次用户做某事时,我想使用 JQuery 将选择框的选项值设置为某个值,例如:

 $("#target").click(function() {
    $("#s_1").val('No'); // #1 is the ID of the select box 1 
});

但它不起作用。我也尝试通过 DocumentFindById 函数检索元素,但它仅在我使用静态 HTML 测试时才能正常工作。当我使用 PHP 动态创建这些选择框时,它会失败。有什么办法可以使这项工作?

谢谢。

添加评论)对不起,我没有在我的代码中使用数字 id。我认为这将是一个更简单的例子。使用字符串 id,它仍然根本不起作用.. :(

4

3 回答 3

0

使用on()如下:

$(document).on('click', '#target', function () {
    $("#1").val('No'); // #1 is the ID of the select box 1 
});
于 2013-04-23T17:45:28.993 回答
0

尝试这个

<script>
   $(function() {
       $("#target").click(function() {
         $("#1").val('No'); // #1 is the ID of the select box 1 
       });
   });
</script>

注意:不要使用 #1 作为 id

于 2013-04-23T17:46:54.863 回答
-1

也许您的代码不适用于动态生成的 html?也许这会起作用:

$("#target").on('click',function() {
    $("#1").val('No'); // #1 is the ID of the select box 1 
});

这里唯一的变化是on

于 2013-04-23T17:43:44.590 回答