日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

button 和input 的区别及在表单form中的用法

發布時間:2023/12/2 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 button 和input 的区别及在表单form中的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

先說一下button 和input的定義:

<button>?標簽定義的是一個按鈕

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

? ? 2、 <button>?控件提供了更為強大的功能和更豐富的內容;

? ? 3、<button>?與?</button>?標簽之間的所有內容都是按鈕的內容,其中包括任何可接受的正文內容,比如文本或多媒體內容。

<input> 標簽規定了用戶可以在其中輸入數據的輸入字段

?? <input> 元素在 <form> 元素中使用,用來聲明允許用戶輸入數據的 input 控件;input具體類型取決于type屬性

接下來具體說明 四種按鈕:? <input type="submit"/>、<input type="button"/>、<button type="submit"></button>、<button type="button"></button>

一、<input type="submit"/>:當用戶單擊此按鈕時,表單會按<form>標記的action屬性設置的方式來發送表單內容。點擊時,頁面會刷新

<form action="#"><input type="text" name="username"/><br/><input type="password" name="password"/><br/><input type="submit" value="登錄"/> </form>

要想在提交數據之前,先對表單數據進行檢驗:

<form action="#" onsubmit="return check()">
用戶名:<input type="text" name="username"/><br/>
密碼:<input type="password" name="password"/><br/>
<input type="submit" value="登錄"/>
</form>
<script LANGUAGE="JavaScript">
function check(){
console.log("提交前先驗證");
var checkElement=document.getElementsByTagName("input");
if(checkElement[0].value==="" || checkElement[1].value==="") {
return false;//當用戶名或者密碼為空時,返回false,此時表單不會提交
}
}
</script>

當check函數里返回false會阻止submit的默認行為,即阻止表單數據提交(阻止頁面刷新)

注意:οnsubmit="return check()"? 中的 return 不能省略

二、<input type="button"/>普通按鈕,必須搭配JS才有用,如onclick事件等

<form action="#" onsubmit="return check()">用戶名:<input type="text" name="username"/><br/>密碼:<input type="password" name="password"/><br/><input type="submit" value="登錄"/><input type="button" value="提醒" onclick="remind()"/> </form> <script LANGUAGE="JavaScript"> function check(){console.log("提交前先驗證");var checkElement=document.getElementsByTagName("input");if(checkElement[0].value==="" || checkElement[1].value==="") {return false;//當用戶名或者密碼為空時返回false,此時表單不會提交 } } function remind(){alert("這是一個簡單按鈕,默認不會提交表單數據,不會刷新頁面"); } </script>

三、<button type="submit"></button>表單數據提交按鈕,與<input type="submit"/>用法相同

<form action="#" onsubmit="return check()">
用戶名:<input type="text" name="username"/><br/>
密碼:<input type="password" name="password"/><br/>
<button type="submit">登錄</button>
</form>
<script LANGUAGE="JavaScript">
function check(){
console.log("提交前先驗證");
var checkElement=document.getElementsByTagName("input");
if(checkElement[0].value==="" || checkElement[1].value==="") {
return false;//當用戶名或者密碼為空時返回false,此時表單不會提交
}
}
</script>

?四、<button type="button"></button>普通按鈕,與<input type="button"/>的用法是一樣的

<form action="#" onsubmit="return check()">用戶名:<input type="text" name="username"/><br/>密碼:<input type="password" name="password"/><br/><button type="submit">登錄</button><button type="button"onclick="remind()">提醒</button> </form> <script LANGUAGE="JavaScript"> function check(){console.log("提交前先驗證");var checkElement=document.getElementsByTagName("input");if(checkElement[0].value==="" || checkElement[1].value==="") {return false;//當用戶名或者密碼為空時返回false,此時表單不會提交 } } function remind(){alert("這是一個簡單按鈕,默認不會提交表單數據,不會刷新頁面"); } </script>

注意一點:

當<button>未處于<form>表單中時,其瀏覽器默認的type就是button;

而當<button>處于<form>表單中時,不同的瀏覽器對 <button> 元素的 type 屬性使用不同的默認值;

因此,建議時刻為button設置type值。

總結一下:

  • <button type="submit"></button> 和 <input type="submit"/>用法相同,用作表單數據提交按鈕,默認即會刷新頁面;

  • <button type="button"></button> 和 <input type="button"/>的用法是一樣的,均為普通按鈕,默認情況不會刷新頁面。

?

總結

以上是生活随笔為你收集整理的button 和input 的区别及在表单form中的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。