[英]jQuery change text box value not working
I'm trying to display total price in sum
label by multiplying qtyTxt
value (user entered) with price
. 我试图通过将
qtyTxt
值(用户输入的)乘以price来在sum
标签中显示price
。 But it doesn't work, the if
statement is never reached. 但是它不起作用,永远不会到达
if
语句。
<input type="text" name="qtyTxt" value="1" size="2" style="width: 25px"/>
<label id="price">${adClicked.price}</label>
<label id="sum">${adClicked.price}</label>
<input type="hidden" name="id" value="${adClicked.id}"/>
<script>
$(".qtyTxt").bind('change', function () {
var sum=$(("#price").val())* ($(this).val());
$("#sum").val('Rs.'+sum);
});
</script>
There are no errors in the console and $("#sum").val('Rs.'+sum);
控制台和
$("#sum").val('Rs.'+sum);
中没有错误$("#sum").val('Rs.'+sum);
works fine. 工作正常。
1) Use parseInt()
or parseFloat()
The parseInt()
function parses a string and returns an integer. 1)使用
parseInt()
或parseFloat()
parseInt()
函数解析一个字符串并返回一个整数。
2) Another mistake Don't use val()
for label instead of use text()
for label 2)另一个错误不要将
val()
用作标签,而不是将text()
用作标签
var sum= parseInt($("#price").text() , 10) * parseInt($(this).val() ,10);
code be 代码是
$(".qtyTxt").bind('change', function () {
var sum= parseInt($("#price").text() , 10) * parseInt($(this).val() ,10);
$("#sum").text('Rs.'+sum);
});
Two issue: 两个问题:
1) You have error in your code, $(("#price").val())
should be $("#price").text()
or $("#price").html()
1)您的代码中有错误,
$(("#price").val())
应该为$("#price").text()
或$("#price").html()
2) You need to parse the values before doing mathematical calculation: 2)您需要在进行数学计算之前解析这些值:
var sum=parseInt($("#price").text(),10)*parseInt($(this).val(),10) ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.