觸發form表單的兩種提交方式,submit和button的用法


1.當輸入用戶名和密碼為空的時候,需要判斷。這時候就用到了校驗用戶名和密碼,這個需要在jsp的前端頁面寫;有兩種方法,一種是用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' action = 'user_login_submit.action' onsubmit = "return checkUser();" > 
<table width="100%" border="0"> 
<tr> 
<td width="60" height="40" align="right">用戶名&nbsp;</td> 
<td><input type="text" value="" class="text2" name = "username" id = "userid"/></td> 
</tr> 
<tr> 
<td width="60" height="40" align="right">&nbsp;&nbsp;&nbsp;</td> 
<td><input type="password" value="" class="text2" name = "userpass" id = "userpassid"/></td> 
</tr> 
<tr> 
<td width="60" height="40" align="right">&nbsp;</td> 
<td><div class="c4"> 
<input type="submit" value="" class="btn2" />

方法二:

 

所有主流瀏覽器都支持 <button> 標簽。

 

定義和用法

 

<button> 標簽定義一個按鈕。

在 button 元素內部,您可以放置內容,比如文本或圖像。這是該元素與使用 input 元素創建的按鈕之間的不同之處。

<button> 控件 與 <input type="button"> 相比,提供了更為強大的功能和更豐富的內容。<button> 與 </button> 標簽之間的所有內容都是按鈕的內容,其中包括任何可接受的正文內容,比如文本或多媒體內容。例如,我們可以在按鈕中包括一個圖像和相關的文本,用它們在按鈕中創建一個吸引人的標記圖像。

唯一禁止使用的元素是圖像映射,因為它對鼠標和鍵盤敏感的動作會干擾表單按鈕的行為。

請始終為按鈕規定 type 屬性。Internet Explorer 的默認類型是 "button",而其他瀏覽器中(包括 W3C 規范)的默認值是 "submit"。

 

重要事項:如果在 HTML 表單中使用 button 元素,不同的瀏覽器會提交不同的值。Internet Explorer 將提交 <button> 與 <button/> 之間的文本,而其他瀏覽器將提交 value 屬性的內容。請在 HTML 表單中使用 input 元素來創建按鈕。

 

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' action = 'user_login_submit.action' >

button按鈕的寫法如下:

<input type="button" value="" class="btn2" onclick = "checkUser();" />


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM