gpt4 book ai didi

javascript - 为什么我的 jquery 选择所有文本值

转载 作者:行者123 更新时间:2023-11-30 07:02:47 25 4
gpt4 key购买 nike

因此,我希望能够从表格单元格中获取一个数字,并将其乘以 focusout 上的输入,但是选择器正在抓取前面名称为 front 的所有表格单元格。这是代码:

j查询

$(document).ready(function(){
$(".percent").focusout(function(){
var val2 = $(this).val();
if (val2==="") {
val2=0;
}
var crew2 = $(this).attr("name"),
front = $(".front, td[name='front"+crew2+"']").text();
console.log(crew2);
console.log(front);
});
});

html:

<tr>
<td class='front' name='frontI210'>$176.00</td>
<td><input type='text' class='percent' name='I210' style='' size='4' /></td>
<td>=</td>
<td class='total' id='I210'></td>
</tr>
<tr>
<td class='front' name='frontI250'>$225.00</td>
<td><input type='text' class='percent' name='I250' style='' size='4' /></td>
<td>=</td>
<td class='total' id='I250'></td>
</tr>

console.log(front) 返回 fronti210 和 fronti250 的所有文本:

$176.00$225.00

我只想从表格单元格中接收与我刚刚完成的输入字段名称相匹配的信息,我该怎么做?

最佳答案

$(".front, td[name='front"+crew2+"']") 搜索所有类为 front 的元素以及所有 td 元素带有 name frontXXX。逗号是 multiple selector [docs] .

要么只搜索具有该名称的 td 元素:

var front = $("td[name='front"+crew2+"']").text();

或者使用 DOM 遍历:

var front = $(this).closest('tr').children('.front').text();

关于javascript - 为什么我的 jquery 选择所有文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com