1

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

jQuery

$(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

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

4

2 回答 2

8

$(".front, td[name='front"+crew2+"']")搜索所有带有 class 的元素front以及所有td带有name frontXXX. 逗号是多重选择器 [docs]

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

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

或使用 DOM 遍历:

var front = $(this).closest('tr').children('.front').text();
于 2012-08-30T19:17:00.830 回答
4
$(".front, td[name='front"+crew2+"']").text();
//this___^ is the culprit.

逗号基本上是说我想要类的front所有元素和具有指定名称的所有 td 元素。

你想要的可能是:

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

以为我还没有测试过,语法可能略有偏差。

于 2012-08-30T19:18:01.657 回答