我有一個帶有2個提交按鈕的窗體。一個是Approve
另一個是Reject
。Javascript數值變量比較不起作用
的HTML is
:
<form action="test.php" method="post" name="sos_submit" id="sos_submit">
<input type="text" name="qty_indented" value="<?php echo $qty_indented ?>" id="qty_indented" readonly="readonly" />
<input type="text" name="qty_approved" value="<?php echo $qty_approved ?>" id="qty_approved" />
<input type="submit" name="da_approve" value="APPROVE" onclick="return submitForm(this)" />
<input type="submit" name="da_reject" value="REJECT" onclick="return submitForm(this)" />
</form>
而且我submitForm(this)
功能樣子:
<script>
function submitForm (button){
if (button.value == "APPROVE"){
var qty_approved = document.getElementById("qty_approved").value;
var qty_intended = document.getElementById("qty_indented").value;
if (qty_approved <= 0){
alert("Please Enter the Quantity for Approval!");
submitOK = "false";
}
else if(qty_approved >= qty_intended){
alert("Quantity Approved " + qty_approved + " Cannot be more than quantity intended " + qty_intended + " !");
submitOK = "false";
}
if (submitOK == "false") {
return false;
}
}
else{
confirm("Are you sure you want to REJECT the Voucher?");
}
}
</script>
雖然它非常簡單的代碼,但不知何故,無法正常工作。
即使qty_approved
是LESS
而不是qty_indented
,if()
語句正在執行。
例如,如果qty_approved = 5
和qty_indented = 10
,那麼代碼應該提交表單,但它不是。它顯示:Quantity Approved 5 Cannot be more than quantity intended 10 !
我做錯了什麼?
'.value'返回一個**字符串**。 'qty_approved> = qty_intended'執行字符串比較。 – 2015-01-15 16:36:32
他們被比較爲字符串 - 嘗試使它們爲int。 – Soren 2015-01-15 16:37:12