我有一個帶有2個文本框的網頁,它包含一個按鈕和一個段落。我想要發生的是如果用戶鍵入一個數字到textbox1然後另一個數字到textbox2並按下名爲計算的按鈕。它會提出一個聲明,說明數字是否低於,高於或等於textbox1中的數字。這是我的,但它沒有工作。它保持不變。如果變量輸入不同,則顯示一條消息
<input type="text" id="textbox1" value="Enter a number" onfocus="javascript:this.value='';">
<input type="text" id="textbox2" value="Enter another number" onfocus="javascript:this.value='';">
<button onclick="calculate()">Calculate</button>
<p id="demo"></p>
<script>
function calculate(){
var x="";
if (textbox1>textbox2){
x="more than";
}
else if (textbox1=textbox2){
x="Same";
}
else{
x="Lower";
}
document.getElementById("demo").innerHTML=x;
}
</script>
有誰知道爲什麼?謝謝
如果我進入'1'和' 1.0'? –
@BradChristie,更好地檢查編輯 –
。儘管爲了性能,你應該存儲轉換後的值。 –