2015-11-19 73 views
0

我製作了一個HTML表單,可以在我的網站上添加一個朋友,我希望能夠通過我提供的鏈接提交。我曾嘗試做onClick="form.submit();",但沒有奏效。我做了一個HTML表單,我想用鏈接提交它

PIC OF WEBSITE

</head> 
<body> 
    <div class="Forum-Block"> 
     <div class="Top-Bar"> 
      <a href="http://site/Forum.php"><div class="Back-Box">Back</div></a> 
      <a href="http://site/ShowFriends.php"><div class="FriendsB-Box">Friends</div></a> 
      <a href="http://site/ShowSentRequests.php"><div class="RequestsB-Box">Sent Requests</div></a> 
      <a href="http://site/AddFriend.php"><div class="SendRequestB-Box">Send Request</div></a> 
     </div> 
     <div class="FriendRequestSend-Box"> 
      <form method="post"> 
       <input type="text" name="friendname"/> 
       <br/> 
       <a href="http://site/AddFriendPHP.php" onclick="form.submit();"><div class="FriendRequestSend-Button">Send</div></a> 
      </form> 
     </div> 
    </div> 
</body> 
+0

爲什麼它不工作,你得到了什麼錯誤,你有沒有嘗試過其他的東西。 http://stackoverflow.com/help/how-to-ask – Mathemats

回答

-1

我會建議使用

<input type="submit" value="submit" /> 
+0

我想知道如果有人知道如何做到這一點,而不是那個 –

+0

它在一個錨標籤內的目的是什麼?你想達到什麼目的? – prola

0

參見:How to submit a form using javascript?

所以基本上給你形成這樣一個名字:

<form method="post" name="theForm">...</form> 

和這樣做的JavaScript的:

document.theFormName.submit(); 

(這樣的鏈接會是這樣的:

<a href="http://site/AddFriendPHP.php" onclick="document.theFormName.submit();"><div class="FriendRequestSend-Button">Send</div></a> 
1

的問題是,form不被任何定義JavaScript變數。你可以給你的<form>標籤命名爲「myFormName」,然後使用document.myFormName從DOM獲取它。一旦你有表單的DOM元素,然後,你可以打電話給submit()

我還應該提到,它通常被認爲是不好的做法,把JavaScript內聯標籤。你可以做到,但這並不理想。相反,您應該將其添加到<script>標記或單獨的.js文件中。

<head> 
    <script> 
     document.getElementById('mySubmitLink').onclick = function() { 
      document.getElementById('myForm').submit(); 
     } 
    </script> 
</head> 
<body> 
... 
<form id="myForm" method="post"> 
    <input type="text" name="friendname"/> 
    <br/> 
    <a href="http://site/AddFriendPHP.php" id="mySubmitLink"><div class="FriendRequestSend-Button">Send</div></a> 
</form>