我有一段代码不起作用。我试图将2个文本框相乘并显示解决方案,但当我单击时,它没有显示任何内容。到目前为止,我的代码是。。。下面的代码可能有什么问题?
<script type="text/javascript">
$('#AddProduct').click(function() {
var totalPrice = $('#debt').val() * $('#income').val();
$('#solution').val() = totalPrice;
});
</script>
<form name="form" method="post" action="">
<table>
<tr>
<td><input type="text" name="income" id="income" /></td>
<td><input type="text" name="debt" id="debt" /></td>
<td><input type="text" id="solution" name="solution" /> </td>
</tr>
</table>
</form>
您的jQuery需要是:
$('#AddProduct').click(function() {
var totalPrice = parseInt($('#debt').val()) * parseInt($('#income').val()); // you can't multiply strings
$('#solution').val(totalPrice); // This is how you use .val() to set the value.
});
这是设置解决方案文本框值的方法:
$('#solution').val(totalPrice);
此外,您的页面上是否真的有一个id为"AddProduct"的元素?
var totalPrice = $('#debt').val() * $('#income').val();
$('#solution').val(totalPrice);
演示
之后,您将jquery添加到页面中,如下所示
<script src="http://code.jquery.com/jquery-latest.js"></script>
您可以执行以下操作:
$('#AddProduct').click(function() {
var totalPrice = parseInt($('#debt').val(),10) * parseInt($('#income').val(),10);
$('#solution').val(totalPrice);
});
你必须告诉它你想要你所针对的输入的value
。并且,始终为parseInt
提供第二个自变量(基数)。它试图过于聪明,如果不提供,就会自动检测,并可能导致意想不到的结果。
提供10
假设您想要一个基数为10的数字。