1. 程式人生 > >關於form表單action屬性的疑問

關於form表單action屬性的疑問

最近看到國外web教程,老師示範了這樣一段程式碼
<html>
  <head>
    <title>Fake Google</title>
  </head>
  <body>
    <div align="center">
      <form name="f" action="https://www.google.com/search">
        <input name="q" type="test"/>
        <br/>
        <input type="submit" value="google搜尋"/>
      </form>
    </div>
  </body>
</html>

開啟後輸入搜尋內容

可以直接通過google進行搜尋

這裡的關鍵在於form表單中的action屬性值,眾所周知,action中寫的是url,來確定表單資料提交的方向,如果不寫則提交當前頁面。

這裡action寫的是

action="https://www.google.com/search"

再看google的原始碼

<form class="tsf" action="/search" ……

比較百度的原始碼

<form id="form" name="f" action="/s"……

當我將action改為

action="https://www.baidu.com/s"

時,輸入搜尋內容後點擊搜尋不再直接進行百度搜索,而是跳轉到百度首頁,搜尋框中內容為空;這是我不明白的地方。

enter鍵提交表單

當input="submit"或"image"時,無論文字框有一個還是多個,提交;

當input="button"時,文字框有一個時,提交;文字框有多個時,不提交;

文字框有一個時不提交的方法:新增一個style="display:none"的text;

問題解決:

通過把將name="wd",問題即可解決;

如下:

<html>
  <head>
    <title>My Baidu</title>
  </head>
  <body>
    <div align="center">
      <form name="f" action="https://www.baidu.com/s" method="get" target="_blank">
        <input name="wd"
id="sbi" type="text" value/> <br/> <input type="submit" value="百度"/> </form> </div> </body> </html>
原理還沒搞清楚