javascript - 使用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());
})
});
解决方案
.find('.price')
会找到<td class="price">
,但它<input class="form-control price">
具有您想要的价值。更改'.price'
为专门选择输入的内容,例如'.form-control.price'
or 'input.price'
。
推荐阅读
- php - 引导下载按钮在新窗口中打开图像而不是下载
- c - 如何在两个 Linux 模块之间共享代码?
- lisp - 在 Common LISP 中更改字节宽度中流
- php - 我想将会话数据发送到 laravel 中的刀片
- azure - Azure 数据工厂:复杂 (JSON) 状态管理
- python - Python TypeError: len() of unsized object
- reactjs - React:如何在包装的组件中获取 HOC 的状态?我可以举一个简单的例子来说明 HOC 是如何传递这样一个值的吗?
- sql - Postgres中以毫秒为单位的下采样时间戳
- hibernate - Spring Boot 没有从 data.sql 文件中插入数据?
- javascript - jQuery插件方法返回对象而不是值