在 jquery 中获取选定 tr 的 td 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2894604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a value of td of a selected tr in jquery
提问by jaeyun
Below is my table
下面是我的桌子
<table>
<tr class=chargeTR>
<td id=chargeTD>
charge1
</td>
</tr class=chargeTR>
<td id=chargeTD>
charge2
</td>
</tr>
<table>
Below is my jQuery call
下面是我的 jQuery 调用
$(".chargeTR").each(function() { // this line works fine
$.get("process.php", {
value: $(this).find("#chargeTD").val(), // I must be doing something wrong here...
}, function(theXML){
alert(theXML);
});
});
I cannot get the value "charge1" and "charge2".
我无法获得值“charge1”和“charge2”。
Can somebody please correct me in this?
有人可以纠正我吗?
回答by Jon Bristow
use .text()
or .html()
instead of .val()
, since .val
is intended to get value=""
attributes from forms.
使用.text()
或.html()
代替.val()
, 因为.val
旨在value=""
从表单中获取属性。
回答by Krishna K
You may also need to use $.trim() to get exact text without whitespace.
您可能还需要使用 $.trim() 来获取没有空格的精确文本。
$.trim($(this).find("#chargeTD").text())
回答by user1676876
It worked form me:
它对我有用:
HTML:
HTML:
<tr class="" id="tr_id" >
<td class="l_id">7283630222</td>
</tr>
<tr class="" id="tr_id" >
<td class="l_id">7276684022</td>
</tr>
<p id="leadID">-lead id here-</p>
jQuery:
jQuery:
$(document).ready(function() {
$("tr#tr_id").click(function() {
$("#leadID").text($(this).find("td.l_id").text());
});
});