[英]Using jquery to get value of text area in closest table cell
I have the following table 我有下表
<tr>
<td data-toggle="tooltip" title="Not Started"><img src="images/small_info_icon.png"></td>
<td>Demo Demo</td>
<td>January</td>
<td class="updaterow"><textarea class="form-control" rows="5" id="notes34455" name="editcomments">Not Started</textarea></td>
<td id="34455"><button type="button" id="updatestatus" class="btn btn-info btn-sm">Update</button> </td>
</tr>
I want to use jquery to get the value of the textarea. 我想使用jquery来获取textarea的值。 I am using the following, but it is giving undefined. 我正在使用以下内容,但未定义。
$("button#updatestatus").click(function(){
var id = $(this).closest('td').attr('id');
var data = $(this).closest("updaterow").text;
alert(data);
var data = {
id: id,
};
$.ajax({
type: "POST",
url: "updatestatus.php",
data: data
}).done(function(msg) {
console.log(msg);
});
Is it possible to get the textvalue of the textarea? 是否有可能获得textarea的textvalue?
.closest()
looks for the closest containing element that matches the selector. .closest()
寻找与选择器匹配的最接近的包含元素。 .updaterow
doesn't contain button#updatestatus
, so it won't find it. .updaterow
不包含button#updatestatus
,因此找不到它。 .updaterow
is the previous td
, so it should be: .updaterow
是先前的td
,因此应为:
var data = $(this).closest("tr").siblings(".updaterow").text();
You also forgot the .
您也忘记了.
in .updaterow
and the ()
in .text()
. .updaterow
的()
和.text()
中的.text()
。
This is what you want 这就是你想要的
$("button#updatestatus").click(function(){
var data = $(this).parent().prev("td.updaterow").find("textarea").val();
alert(data);
});
Here is an example 这是一个例子
$("button#updatestatus").click(function(){ var data = $(this).parent().prev("td.updaterow").find("textarea").val(); alert(data); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <table> <tr> <td data-toggle="tooltip" title="Not Started"><img src="images/small_info_icon.png"></td> <td>Demo Demo</td> <td>January</td> <td class="updaterow"><textarea class="form-control" rows="5" id="notes34455" name="editcomments">Not Started</textarea></td> <td id="34455"><button type="button" id="updatestatus" class="btn btn-info btn-sm">Update</button> </td> </tr> </table> </html>
This is what you need - 这就是您需要的-
$(this).closest('tr').find('textarea').val();
Here is a fiddle for the same - https://jsfiddle.net/v44rzxLo/1/ . 这是相同的小提琴-https: //jsfiddle.net/v44rzxLo/1/ 。 On a side note, do not have same ids for multiple elements. 附带说明一下,多个元素没有相同的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.