当用户输入金额并收到此错误时,我正在尝试运行事件
未捕获的TypeError:无法读取未定义的属性“ toLowerCase”
的HTML
<input type="text" id="ItemQuantity#i#" name="ItemQuantity#i#" onkeypress="return isNumeric(event)" onkeydown="return keyispressed(event);" onchange="autototal()">
试图专注于onchange =“ autototal()”
jQuery查询
function autototal(){
var sum = 0;
var quantity = 0;
var price = 0;
var totalPrice = 0;
quantity = $(this).val();
price = $(this).closest('.col-price').find('input').val();
console.log(quantity);
console.log(price);
}
最佳答案
调用处理程序的方式this
不是元素,而是window
。
使用jQuery连接处理程序,这将确保this
引用元素:
$("[id='ItemQuantity#i#']").on("change", autototal);
...或者如果您真的想使用
onxyz
属性样式的事件处理,请执行以下操作:<input ... onchange="autototal.call(this)">
并且应该按原样工作,或执行以下操作:
onchange="autototal(this)"
...并更新
autototal
以使用参数而不是this
。