Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery计数百分比-显示;“未定义”;_Jquery_Html_Input_Percentage_Jquery Calculation - Fatal编程技术网

jQuery计数百分比-显示;“未定义”;

jQuery计数百分比-显示;“未定义”;,jquery,html,input,percentage,jquery-calculation,Jquery,Html,Input,Percentage,Jquery Calculation,我的脚本应该统计表中的值,但不能替换“%”字符并显示“未定义”。我尝试了一些解决办法,但没有成功 我拉了一把小提琴: 它工作正常,直到第行:$(this.find('input.wartosc_netto').val(wartosc_netto)我想我找到了。。。您对所有行执行一个each。第一行是标题。所以它不会找到任何值。将该行替换为,则不会出现未定义的错误。当发生这样的错误时,脚本的其余部分将停止 更好的方法是给一个类,然后循环遍历这些类 请注意,如果在执行替换之前找不到任何值,请后退一步

我的脚本应该统计表中的值,但不能替换“%”字符并显示“未定义”。我尝试了一些解决办法,但没有成功

我拉了一把小提琴:


它工作正常,直到第行:
$(this.find('input.wartosc_netto').val(wartosc_netto)

我想我找到了。。。您对所有
行执行一个each。第一行是标题。所以它不会找到任何值。将该行替换为
,则不会出现
未定义的
错误。当发生这样的错误时,脚本的其余部分将停止

更好的方法是给
一个类,然后循环遍历这些类


请注意,如果在执行替换之前找不到任何值,请后退一步

阿尔文·巴克尔(Alvin Bakker)离目标很近,但它需要进一步调整

问题是,您会在每一行上运行,包括标题行。标题行不包含所需的值,因此它会遇到错误,javascript会在出现错误时停止执行

解决此问题的一个简单方法是检查您是否在标题行中:

  $('table .ilosc, table .cena_netto .stawka_vat').on('keyup paste change', function() {
        $('table tr').each(function() {

            //NEW CODE HERE:
            //Pseudo code: if this row contains <th>-elements, 
            //then don't bother with it and move on to the next row.
            if ($(this).find('th').length > 0)
               return;

            var ilosc = $(this).find('input.ilosc').val();
            var cena_netto = $(this).find('input.cena_netto').val();
            var wartosc_netto = (ilosc * cena_netto);
            $(this).find('input.wartosc_netto').val(wartosc_netto);
            var stawka_vat = $(this).find('input.stawka_vat').val().replace('%', '');
            var  kwota_vat = ( wartosc_netto * (stawka_vat / 100) );
            $(this).find('input.kwota_vat').val(kwota_vat);
            var wartosc_brutto = (wartosc_netto + kwota_vat);
            $(this).find('input.wartosc_brutto').val(wartosc_brutto);
        }); //END .each
    return false;
}); // END change

再次:感谢SlashmanX指出了这一点。

您确定该值是INT吗?不是,可能我需要解析它,但它在“replace”之后显示“undefined”。但我不知道为什么…首先将ilosc和cena_netto设置为INT,然后进行计算。你不能有关于not INT VALUES的公式谢谢你Alvin的帮助,但是看,这很好:第一个问题是当我使用“replace”时。有%的值永远不是INT。删除%后做一个parseInt,你不能用
替换
,它需要更多的工作。难道不能用
替换第一个
吗?@SlashmanX不是你想象的那样:在
内部,他需要他现在拥有的
(请参阅)。然后他还需要一个
来放置内容行,然后他可以只过滤
。但是这是一个简单的更改,从语义上来说更正确,因为它是表的开头。只需将其选择器替换为
table tbody tr
@SlashmanX是的,我理解你的意思,我编辑了我的答案以指出这一点。:)
  $('table .ilosc, table .cena_netto .stawka_vat').on('keyup paste change', function() {
        $('table tr').each(function() {

            //NEW CODE HERE:
            //Pseudo code: if this row contains <th>-elements, 
            //then don't bother with it and move on to the next row.
            if ($(this).find('th').length > 0)
               return;

            var ilosc = $(this).find('input.ilosc').val();
            var cena_netto = $(this).find('input.cena_netto').val();
            var wartosc_netto = (ilosc * cena_netto);
            $(this).find('input.wartosc_netto').val(wartosc_netto);
            var stawka_vat = $(this).find('input.stawka_vat').val().replace('%', '');
            var  kwota_vat = ( wartosc_netto * (stawka_vat / 100) );
            $(this).find('input.kwota_vat').val(kwota_vat);
            var wartosc_brutto = (wartosc_netto + kwota_vat);
            $(this).find('input.wartosc_brutto').val(wartosc_brutto);
        }); //END .each
    return false;
}); // END change
<table> 
    <thead>
        <tr>
            <th>Lp.</th>
            <th>Nazwa towaru lub usługi</th>
            <th>Jednostka</th>
            <th>Ilość</th>
            <th>Cena netto</th>
            <th>Wartość netto</th>
            <th>Stawka VAT</th>
            <th>Kwota VAT</th>
            <th>Wartość brutto</th>
        </tr>
    </thead>
    <!-- All data rows below -->
</table>
 $('table .ilosc, table .cena_netto .stawka_vat').on('keyup paste change', function() {

        //Subtle difference in this line: Spot the '>' in the selector!
        $('table>tr').each(function() {
            var ilosc = $(this).find('input.ilosc').val();
            var cena_netto = $(this).find('input.cena_netto').val();
            var wartosc_netto = (ilosc * cena_netto);
            $(this).find('input.wartosc_netto').val(wartosc_netto);
            var stawka_vat = $(this).find('input.stawka_vat').val().replace('%', '');
            var  kwota_vat = ( wartosc_netto * (stawka_vat / 100) );
            $(this).find('input.kwota_vat').val(kwota_vat);
            var wartosc_brutto = (wartosc_netto + kwota_vat);
            $(this).find('input.wartosc_brutto').val(wartosc_brutto);
        }); //END .each
    return false;
}); // END change