Re: [問題] 我這樣判斷負值對嗎?

看板Web_Design作者 (卡氏)時間18年前 (2007/05/08 13:56), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/2 (看更多)
看原文似乎是在討論 JavaScript 參考以下範例 <html> <head> <SCRIPT language=javascript> function test(){ var strClass = document.forms[0].money.value; // 或是 if(strClass.valueOf() < 0) // parseInt 語法參考 http://0rz.pixnet.tw/772Ex if(parseInt(strClass) < 0){ alert("負值"); } else{ alert("正值"); } } </script> </head> <body> <form name="form" > <input type="text" name="money" value=50> <input type="button" name="Go" value="GO" onClick="test()" /> </form> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.88.89

05/09 07:31, , 1F
建議用parseInt(x,10),因為沒有指定是幾進位可能會誤判
05/09 07:31, 1F

05/09 07:34, , 2F
可以試試看不指定10進位的時候,轉換"01"~"09"會出現什麼結果
05/09 07:34, 2F
文章代碼(AID): #16G13zlA (Web_Design)
文章代碼(AID): #16G13zlA (Web_Design)