在html靜態頁面中為button加上提交連結的方法介紹

黄舟
發布: 2017-07-27 14:15:22
原創
2200 人瀏覽過

1、按鈕做成連結(圖片)的樣子
提交按鈕

<input type="submit" value="提交">
登入後複製

提交連結

<a href="#" onclick="表单名字.submit()">提交</a>
登入後複製

重設按鈕

<input type="reset" value="重置">
登入後複製

重設連結

<a href="#" onclick="表单名字.reset()">重置</a>
登入後複製

普通按鈕

<input type="button" value="按钮" onclick="函数()">
登入後複製

普通連結

<a href="#" onclick="函数()">链接</a>
登入後複製

至於圖片也一樣把a標籤換成img
2、連結做成按鈕的樣子

#
<a href="reg.asp">注册</a>
=><input type="button" value="注册" onclick="location.href=&#39;reg.asp&#39;">
登入後複製

-----------------------------------
有的時候我們完全可以手工做一個get方式的表單,至於用按鈕還是連結隨心所欲。

<form action="xx.asp" method="get" name="form1">
  <input name="aa" type="text" id="aa">
  <input name="bb" type="text" id="bb">
  <input type="submit" name="Submit" value="提交">
</form>
=>
<input name="aa" type="text" id="aa">
<input name="bb" type="text" id="bb">
<input type="button" value="按钮" onclick="location.href=&#39;xx.asp?aa=&#39;+document.all[&#39;aa&#39;].value+&#39;&bb=&#39;+document.all[&#39;bb&#39;].value">
登入後複製

--------------------------------------------------##進一步說我們也可以做一個按鈕(連結)來同時傳遞js變量,表單input的值,asp變量,Recordset值

<script language="javascript">
var id1=1;
</script>
<%
id3=3
....
rs.open exec,conn,1,1
假设有rs("id4")=4
...
%>
<input name="id2" type="text" id="id2" value="2">
<input type="button" value="按钮"
onclick="location.href=&#39;xx.asp?id1=&#39;+id1+&#39;&id2=&#39;+document.all[&#39;id2&#39;].value+&#39;&id3=<%=id3%>&id4=<%=rs("id4")%>&#39;">
登入後複製

我們按下按鈕會看到瀏覽器的url是xx.asp ?id1=1&id2=2&id3=3&id4=4

在xx.asp中我們就可以用request.querystring來得到所有變數,這樣是不是變相的客戶端js和伺服器段的變數傳遞?
----------------------------------------------- -------------------------------------------------- -----------------------------
如何為按鈕加上連結功能

解決想法:
按鈕屬於控制項的對象,優先順序比較高,所以不能像圖片或文字一樣直接加鏈接,只能透過按鈕的點選事件呼叫腳本的方式來實現。
特定步驟:
    1.在原始視窗開啟連結

    <input type="button" 
value="闪吧" onClick="location=’http://www.xxx.net’">
    <button onClick="location.href=’http://www.xxxx.net’">闪吧</button>
    <form action="http://www.xxxx.net%22%3e%3cinput/ type="submit" value="打开链接"></form>
登入後複製

    2.在新視窗中開啟連結


<input type="button" 
value="闪吧" onClick="window.open(’http://www.xxxx.net’)">
    <button onClick="window.open(’http://www.xxxx.net’)">ggg</button>
    <form action="http://www.xxxx.net/" 
target="_blank"><input type="submit" value="打开链接"></form>
登入後複製

注意:onClick呼叫的程式碼裡的引號在只有一重時可以單雙嵌套,超過兩重就必須用"\"號轉義且轉義的引號必須跟裡層的引號一致,如:

<button onClick="this.innerHTML=’<font color=\’red\’>http://www.xxxx.net</font>’">闪吧</button>
登入後複製


<button onClick=’this.innerHTML="<font color=\"red\">http://www.xxxx.net</font>"’>闪吧</button>
登入後複製

而下面都是錯誤的寫法:


<button onClick="this.innerHTML=’<font color=’red’>http://www.xxxx.net</font>’">闪吧</button>
<button onClick="this.innerHTML=’<font color="red">http://www.xxxx.net</font>’">闪吧</button>
<button onClick="this.innerHTML=’<font color=\"red\">http://www.xxxx.net</font>’">闪吧</button>
登入後複製

 提示:大部分屬於window或document物件的方法和屬性都可以省略前綴window或document,比如說本例中的location.href(location.href又可以簡寫為location,因為location的預設物件為href)就是window.location.href或document.location.href的省略式寫法。


技巧:本例中也可以用下面的方法來取代location.href

location.replace(url)
location.assign(url)
navigate(url)
登入後複製
特別提示


第一步中的程式碼執行後,單擊按鈕將跳到連結目標。而第二步的在點擊按鈕後將在新視窗中開啟連結。

特別說明

本例主要是透過用onClick捕獲使用者在按鈕上的點擊事件,然後呼叫location物件的href方法或window物件的open方法來開啟連結。另外一個技巧是透過提交表單來實現連結功能,按鈕必須是type=submit類型的按鈕,表單的action值就是連結目標,target值就是連結開啟的目標方式。

以上是在html靜態頁面中為button加上提交連結的方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!