简体   繁体   English

如何通过<a href...> ... </a>链接进行提交?

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

I got an image with which links to another page using <a href="..."> <img ...> </a> . 我有一个图像,使用<a href="..."> <img ...> </a>链接到另一个页面。

How can I make it make a post like if it was a button <input type="submit"...> ? 我怎样才能让它像一个按钮<input type="submit"...>一样发帖?

More generic approatch using JQuery library closest () and submit () buttons. 更通用approatch使用JQuery的最近的 (),并提交 ()按钮。 Here you do not have to specify whitch form you want to submit, submits the form it is in. 在这里,您不必指定要提交的表格,提交表格。

<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坐标。

It looks like you're trying to use an image to submit a form... in that case use <input type="image" src="..."> 看起来您正在尝试使用图像提交表单...在这种情况下使用<input type="image" src="...">

If you really want to use an anchor then you have to use javascript: 如果你真的想使用锚,那么你必须使用javascript:

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

输入类型=图像将为您完成。

Untested / could be better: 未经测试/可能更好:

<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.

What might be a handy addition to this is the possibility to change the post-url from the extra button so you can post to different urls with different buttons. 可能是一个方便的补充是可以从额外按钮更改后url,以便您可以使用不同的按钮发布到不同的URL。 This can be achieved by setting the form 'action' property. 这可以通过设置表单'action'属性来实现。 Here's the code for that when using jQuery: 这是使用jQuery时的代码:

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

The action-attribute has some issues with older jQuery versions, but on the latest you'll be good to go. action-attribute与旧的jQuery版本有一些问题,但是最新的你会很高兴。

Something like this page ? 像这个页面的东西?

<!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内...

We replace the submit button with this all the time on forms: 我们一直在表单上替换提交按钮:

<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>

Clicking the image submits the form. 单击图像会提交表单。 Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM