1
<table class="csstable">
    <tr>
       <td>
       <textarea id="txtOption" rows="2" cols="30">hi this is me!!!</textarea>
       </td>
    </tr>
    <tr>
       <td>
       <textarea id="txtOption" rows="2" cols="30">hi this is you!!!</textarea>
       </td>
    </tr>

我做了什么:

  $(".csstable").each(function(){
       alert($("#txtOption").val());
      });

我在这两个时间都得到了文本区域的第一个值,即。嗨,这是我!!!

4

3 回答 3

1

一个页面上不能有两个具有相同值的 id,这是无效的。

ID 必须是唯一的。

尝试使用一个类(或其他东西)来选择

<textarea class="txtoption1" rows="2" cols="30">option1</textarea>

<textarea class="txtoption2" rows="2" cols="30">option2</textarea>
于 2012-12-07T19:27:18.610 回答
1

当您有多个具有相同 id 的元素时,您将只匹配第一个元素。

因此,请改用class标签名称或使用唯一 ID 获取它。

然后您可以通过以下方式获得它的价值。

$(".csstable .myTextareaClass").each(function(){
    alert($(this).val());
});

或者

$(".csstable textarea").each(function(){
    alert($(this).val());
});

或者

alert($('#textareaId').val());
于 2012-12-07T19:29:35.753 回答
0

每个 id 必须是唯一的,因为它被调用id是有原因的。

相反,您应该使用class属性

<textarea class="txtOption" rows="2" cols="30">hi this is you!!!</textarea>

你可以在这里阅读更多关于它的信息

于 2012-12-07T19:27:54.660 回答