簡體   English   中英

如何通過<a href...> ... </a>鏈接進行提交?

[英]How to make a submit out of a <a href…>…</a> link?

我有一個圖像,使用<a href="..."> <img ...> </a>鏈接到另一個頁面。

我怎樣才能讓它像一個按鈕<input type="submit"...>一樣發帖?

更通用approatch使用JQuery的最近的 (),並提交 ()按鈕。 在這里,您不必指定要提交的表格,提交表格。

<a href="#" onclick="$(this).closest('form').submit()">Submit Link</a>
<input type="image" name="your_image_name" src="your_image_url.png" />

這將在提交表單時發送your_image_name.xyour_image_name.y值,這是用戶單擊圖像的位置的x和y坐標。

看起來您正在嘗試使用圖像提交表單...在這種情況下使用<input type="image" src="...">

如果你真的想使用錨,那么你必須使用javascript:

<a href="#" onclick="document.forms['myFormName'].submit(); return false;">...</a>

輸入類型=圖像將為您完成。

未經測試/可能更好:

<form action="page-you're-submitting-to.html" method="POST">
    <a href="#" onclick="document.forms[0].submit();return false;"><img src="whatever.jpg" /></a>
</form>
 <html>

 <?php

 echo $_POST['c']." | ".$_POST['d']." | ".$_POST['e'];

 ?>

 <form action="test.php" method="POST">
      <input type="hidden" name="c" value="toto98">
      <input type="hidden" name="d" value="toto97">
      <input type="hidden" name="e" value="toto aaaaaaaaaaaaaaaaaaaa">

      <a href="" onclick="document.forms[0].submit();return false;">Click</a> 
 </form>

</html>


So easy.




So easy.

可能是一個方便的補充是可以從額外按鈕更改后url,以便您可以使用不同的按鈕發布到不同的URL。 這可以通過設置表單'action'屬性來實現。 這是使用jQuery時的代碼:

$('#[href button name]').click(function(e) {
    e.preventDefault();
    $('#[form name]').attr('action', 'alternateurl.php');
    $('#[form name]').submit();
});

action-attribute與舊的jQuery版本有一些問題,但是最新的你會很高興。

像這個頁面的東西?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>BSO Communication</title>

<style type="text/css">
.submit {
    border : 0;
    background : url(ok.gif) left top no-repeat;
    height : 24px;
    width : 24px;
    cursor : pointer;
    text-indent : -9999px;
}
html:first-child .submit {
    padding-left : 1000px;
}
</style>
<!--[if IE]>
<style type="text/css">
.submit {
    text-indent : 0;
    color : expression(this.value = '');
}
</style>
<![endif]-->
</head>

<body>
    <h1>Display input submit as image with CSS</h1>

    <p>Take a look at <a href="/2007/07/26/afficher-un-input-submit-comme-une-image/">the related article</a> (in french).</p>
    <form action="" method="get">
        <fieldset>
            <legend>Some form</legend>
            <p class="field">
                <label for="input">Some value</label>

                <input type="text" id="input" name="value" />
                <input type="submit" class="submit" />
            </p>
        </fieldset>
    </form>

    <hr />
    <p>This page is part of the <a href="http://www.bsohq.fr">BSO Communication blog</a>.</p>

</body>
</html>

不要忘記“BUTTON”元素可以處理更多的HTML內...

我們一直在表單上替換提交按鈕:

<form method="post" action="whatever.asp">
<input type=...n

<input type="image" name="Submit" src="/graphics/continue.gif" align="middle" border="0" alt="Continue">
</form>

單擊圖像會提交表單。 希望有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM