2012-10-29 25 views
0

嗨,下面是我的代碼的一部分。我沒有得到所需的else if (txt3Val.value >= txt4Val),但如果我把它調整到else if (document.getElementById('txt3') >= document.getElementById('txt4'))它的作品。有人能告訴我爲什麼發生這種情況嗎謝謝。附加到Javascript中的變量的讀值

......AND THE CODE READS 
    else if(document.form1.GWchk.checked == true) 
     { 
     var txt3Val = document.getElementById('txt3'); 
     var txt4Val = document.getElementById('txt4'); 
      if(txt3Val.value == "" || txt4Val.value == "") 
      { 
       alert ("You need to enter both starting and ending number \nof the cheque book for GlW."); return false; 
      } 
      else if (txt3Val.value >= txt4Val) 
      { 
       alert ("Ending cheque number should be of greater value"); return false; 
      } 
      else 
      { 
      alert ("Successfully saved your entry1."); return false; 
      } 
     } 

亮點:對不起代碼else if (txt3Val.value >= txt4Val.value)也不起作用!其實這就是它在我的真實腳本上的重寫錯誤。但是,我的前期保持不變,它不會給我結尾號碼應該是greate ....直接去成功保存。

編輯我想我找出了一些東西。在第一個文本框中,如果我把'10'作爲一個值,第二個作爲'9',那麼JS似乎不會將9識別爲較低的值。但是如果我把'09'給出了想要的輸出。我該如何處理?

+0

你將如何比較兩個HTML元素?根據性質使用相應的成員,即'''''' –

+0

您的意思是'txt4Val.value'在if語句中嗎? – jrajav

+0

不應該是「else if(txt3Val.value> = txt4Val.value)」而不是「else if(txt3Val.value> = txt4Val)」? – AnandVeeramani

回答

0
if(parseInt(txt3Val.value, 10) >= parseInt(txt4Val.value, 10)) 
2
else if (txt3Val.value >= txt4Val) 

應該

else if (txt3Val.value >= txt4Val.value) 
0

使用比較重的HTML元素是不會給你你想要的結果。追加.valuetxt4Val末從元素訪問值:

else if (txt3Val.value >= txt4Val.value) { 

} 
3

嘗試改變:

else if (txt3Val.value >= txt4Val) 

else if (txt3Val.value >= txt4Val.value)