2015-11-19 70 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> 
相关问题