使用js提交form表單的兩種方法

來源:互聯網
上載者:User

標籤:

提交form表單的時候瑤族一些簡單的驗證,驗證完後才能提交,避免無效提交。

1.當輸入使用者名稱和密碼為空白的時候,需要判斷。這時候就用到了校正使用者名稱和密碼,這個需要在前端頁面寫;有兩種方法,一種是用submit提交。一種是用button提交。
方法一:

在jsp的前端頁面的頭部插入一個js方法:

function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("userpassid").value;
if(result == "" ){
alert("使用者名稱不可為空");
return false;
}
if(password == "" ){
alert("密碼不可為空");
return false;
}else{
return true;
}
}

在form表單裡寫成這樣:

<form id="formid" name="myform" method=‘post‘ onsubmit="return checkUser();" >
<input type="text" value="" class="text2" name="username" id="userid"/></td>
<input type="password" value="" class="text2" name="userpass" id="userpassid"/></td>
<input type="submit" value="" class="btn2" />
</form>

方法二:

function checkUser(){
var result = document.getElementById("userid").value;
var password = document.getElementById("passid").value;

if(!result){
alert("使用者名稱不可為空");
return false;
}
if(!password){
alert("密碼不可為空");
return false;
}
document.getElementById("formid").submit();
}

form表格的寫法,需要寫id
<form id="formid" method=‘post‘>

button按鈕的寫法如下:
<input type="button" value="" class="btn2" onclick="checkUser();" />

使用js提交form表單的兩種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.