这是用于计算问题所在项目价格的javascript 只要价格是4位数,返回的值就是NaN。
这是我隐藏的价格区域:
<input type="hidden" name="price" id="price"class="price" value="4500"readonly >
这是我的数量字段
<input type="number" name="quant" id="quant" value="2" />
这是我的运费
<select id="shipment" onchange="myFunction3()" name="shipment2" disabled>
<option value="100" data-quantity="1">1 for 100 pesos </option>
</select
这是总价
<input type="text" id="demo" name="total_price" style="margin-top:10px;margin-left:5px;" readonly>
更改货件价值的脚本
<script type="text/javascript">
document.getElementById('quant').addEventListener("keyup", function(){
var value = parseInt(this.value, 20),
selectEle = document.getElementsByTagName('select')[0],
options = selectEle.options,
selectedNum = 0;
for(var i = 0; i < options.length; i++) {
//checking the exact string with spaces (" " + value + " ")
if(options[i].textContent.indexOf(" " + value + " ") > -1) {
selectedNum = i;
}
}
selectEle.selectedIndex = selectedNum ? selectedNum : 0;
}, false);
</script>
计算所有值
function myFunction3() {
var y= document.getElementById("shipment").value;
return y;
}
<script>
$("#price,#quant,#shipment").keyup(function () {
if(+myFunction3() =="" )
{
$('#demo').val(0);
}
else if($('#trigger')=="checked") //this is the problem
{
$('#demo').val($('#price').val() * $('#quant').val() ;
}
else
{
$('#demo').val($('#price').val() * $('#quant').val() + +myFunction3());
}
});
</script>
答案 0 :(得分:1)
不确定这里是否只是输错了,但是问题区域附近有语法错误(缺少右括号):
$('#demo').val($('#price').val() * $('#quant').val() ;
应该是:
$('#demo').val($('#price').val() * $('#quant').val());
我认为在你对它们进行数学运算之前确保你不使用字符串会好得多:
var price = parseInt($('#price').val(), 10);
var quantity = parseInt($('#quant').val(), 10);
$('#demo').val(price * quantity);
您可以进一步确保在使用它们之前它们都不是NaN
:
var price = parseInt($('#price').val(), 10);
var quantity = parseInt($('#quant').val(), 10);
if(!isNaN(price) && !isNaN(quantity)) {
$('#demo').val(price * quantity);
} else {
alert('Please enter numbers only!');
}