使用 jquery 更新当前行中的值

Updating values in current row using jquery

本文关键字:jquery 更新 使用      更新时间:2023-09-26

嗨,我有这个从上一篇文章中修复的表单。

<form id="eForm" action="">
    <table class="fMain" border="1" cellpadding="0" cellspacing="0">
        <tbody>        
            <tr class="ist">
                <td>In work</td>
                <td>09/25/2013</td>
                <td><input type="text" value="3500.00" name="salesS" class="totalS fo" readonly=""></td>                
                <td ><input type="text" value="23.00" name="cusS" class="cusS form-field-tiny readonly" readonly=""></td>
                <td>0.66%</td>                                         
            </tr>
            <tr class="ist">
                <td>In work</td>
                <td>09/25/2013</td>
                <td><input type="text" value="2445.00" name="salesS" class="totalS fo" readonly=""></td>                
                <td ><input type="text" value="444.00" name="cusS" class="cusS form-field-tiny readonly" readonly=""></td>
                <td>0.66%</td>                     
            </tr>                             
        </tbody>
    </table>
</form>

我正在使用以下jQuery进行一些格式化。

$('.eForm tr.ist').each(function () {
    var num = $(this).find('#salesS').val();
    var cNum = $(this).find('#cusS').val();
    alert(num);
    alert(cNum);
    document.getElementById('salesS').value = pC(num);
    document.getElementById('cusS').value = pC(cNum);
});

它工作正常,所有值都处理良好。但是当它使用以下行它不会更新当前行。它只是更新顶行。

document.getElementById('salesS').value = pC(num);
document.getElementById('cusS').value = pC(cNum);

我试过这个

$(this).find('#cusS').val()=pC(num);

它没有用。请让我知道如何修复它,以便它更新当前行中的值。谢谢

你的jQuery是非常错误的。原因如下:

$('.eForm tr.fMain').each(function () { // YOU GOT NO TR WITH THAT CLASS, It'S A TABLE
    var num = $(this).find('#salesS').val(); // YOU GOT NO ELEMENT WITH "salesS" ID
    var cNum = $(this).find('#cusS').val();// YOU GOT NO ELEMENT WITH "cusS" ID
    alert(num);
    alert(cNum);
    document.getElementById('salesS').value = pC(num); 
    document.getElementById('cusS').value = pC(cNum);
});

这段代码也是错误的:

$(this).find('#cusS').val()=pC(num); 

它可能像这样有效(如果您有带有 cus5 id 的输入

$(this).find('#cusS').val(pC(num));

现在你可能想要的是:

$('.eForm tr.ist').each(function () { 
    var inputSales = $(this).find('input[name=salesS]');
    var inputCus =  $(this).find('input[name=cusS]');
    inputSales.val(pC(inputSales.val());
    inputCus.val(pC(inputCus.val());
});