網(wǎng)頁Form只有一個(gè)input框時(shí),按回車后表單就直接提交了,輸入檢測函數(shù)不起作用?
完善時(shí)間:2013-03-13 15:34:22會員:貝加達(dá)目錄:電腦網(wǎng)絡(luò) - 網(wǎng)站建設(shè)瀏覽:6527 次
下例中的 TestForm 表單中只有一個(gè)名稱為 TestInput 輸入框,當(dāng) TestInput 輸入框處于聚焦?fàn)顟B(tài)時(shí),按回車表單直接就提交了,而沒有通過輸入檢測函數(shù) CheckInput() 進(jìn)行合法性檢查,當(dāng)表單中有多個(gè)輸入框時(shí)沒有此問題,請問這種問題有沒有什么辦法解決?
<form name="TestForm" method="post" action="test.asp?act=test"> 請輸入6位數(shù)字:<input name="TestInput" type="text" tabindex="1" maxlength="6"> <a href="javascript:void(0);" onclick="javascript:CheckInput();return false;">測試</a> </form> <script type="text/javascript"> //輸入檢測函數(shù) function CheckInput(){ if(!(/^(\d{6})$/).test(document.TestForm.TestInput.value)){ alert("請檢查輸入是否有誤!"); document.TestForm.TestInput.focus(); return false; } document.TestForm.submit(); return true; } </script>
第1條回答
完善時(shí)間:2013-03-13 15:51:46會員:倩華
你可以嘗試以下方法來解決:
1、在 Form 中加入提交檢測
<form name="TestForm" method="post" action="test.asp?act=test" onsubmit="javascript:CheckInput();return false;"> 請輸入6位數(shù)字:<input name="TestInput" type="text" tabindex="1" maxlength="6"> <a href="javascript:void(0);" onclick="javascript:CheckInput();return false;">測試</a> </form>
2、加一個(gè)不顯示的輸入框
<form name="TestForm" method="post" action="test.asp?act=test"> 請輸入6位數(shù)字:<input name="TestInput" type="text" tabindex="1" maxlength="6"> <input type="text" name="FixEnter" style="display:none;"> <a href="javascript:void(0);" onclick="javascript:CheckInput();return false;">測試</a> </form>
3、在輸入框加入回車檢測:
<form name="TestForm" method="post" action="test.asp?act=test"> 請輸入6位數(shù)字:<input name="TestInput" type="text" tabindex="1" maxlength="6" onkeydown="javascript:if(event.keyCode==13){CheckInput();return false;}"> <a href="javascript:void(0);" onclick="javascript:CheckInput();return false;">測試</a> </form>
個(gè)人覺得優(yōu)先使用第1種、第2種方法,第3種方法跟蹤并判斷onkeydown擊鍵事件,效率較低,不推薦使用。
發(fā)布日期:2020/01/15
發(fā)布日期:2019/07/22
發(fā)布日期:2019/03/17
蘇公網(wǎng)安備 32011102010041號
320111201705278429
蘇ICP備12027725號