一 如果<form></form>表單中只有一個<input type="text"/>,則使文本框獲取焦點,並單擊回車,form會自動提交。
提交路徑為action屬性拼接到當前路徑(action屬性默認為空字符串,如果form沒有action屬性,則提交路徑與當前路徑相同)。
瀏覽器表現:Chrome(桌布版、移動版)會出現此問題,Android手機會出現。Safari(桌面版、移動版)不會出現。
解決方法:禁止表單提交。
1 設置成<form onsubmit="return false;">
2 增加一個無name屬性的隱藏文本框 <input type="text" style="display:none;/>
3 監聽input的keydown事件。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Demo</title> </head> <body> <form > <input type="text" name="username"/> </form> </body> </html>
