首页 > 解决方案 > 使用keyup检索输入框的值 - JS

问题描述

在我下面的代码中,这就是我想要实现的。我有两个输入框,数量和价格。它们都不是固定的,这意味着客户/用户可以输入他们想要的任何数字,并且代码必须相乘才能得出总数。

下面是我的代码,数量框似乎工作正常,但我无法在价格框中获取我的价格值。我得到的总数是 NAN。

我在我的代码中做错了什么,为什么 keyup 对我的价格输入框不起作用?

PS:javascript初学者,请原谅我的英语

<div class="checkout_panel">
<div class="panel_checkout">
</div>

</div>

  '<div class="order_container" style=" font-size:14px; "> '+ 

    '<td  class="qtyhtml" ><input  type="text" style="width:55px;text-align:left;"  value="1" class="form-control quantity" id="qty" placeholder=" qty " name="quantity[]"  required/></td>'+
    '<td  class="price" ><input  type="text"  style="width:55px;text-align:left;"  value="'+ad.retail_price+'" class="form-control price" id="price" placeholder=" qty " name="price[]"  required/></td>'+


    '<td><p class="total" >GHS<span class="line-total" name="total" id="total"></span></p></td>'+
       '</tr>'+
    '</tbody>'+
    '</table>'+
    '</div>'

    $('.checkout_panel').on('keyup','.quantity',function()

           {

           order_container = $(this).closest('div');

           quantity = Number($(this).val());
           price =  
           Number($(this).closest('div').find('.price').val());

           order_container.find(".total span").text(quantity * price);

            sum = 0;

           $(".line-total").each(function(){
           sum = sum + Number($(this).text());

           })   





        });

标签: javascript

解决方案


.find('.price')会找到<td class="price">,但它<input class="form-control price">具有您想要的价值。更改'.price'为专门选择输入的内容,例如'.form-control.price'or 'input.price'


推荐阅读