59

我得到了一张图片,其中使用<a href="..."> <img ...> </a>.

我怎样才能让它像一个按钮一样发布<input type="submit"...>

4

10 回答 10

118

使用JQuery最接近()和提交()按钮的更通用的方法。在这里,您不必指定要提交的表单,提交它所在的表单。

<a href="#" onclick="$(this).closest('form').submit()">Submit Link</a>
于 2011-11-03T15:26:11.460 回答
34
<input type="image" name="your_image_name" src="your_image_url.png" />

这将在提交表单时发送your_image_name.xyour_image_name.y值,它们是用户单击图像位置的 x 和 y 坐标。

于 2008-11-08T19:01:29.030 回答
28

看起来您正在尝试使用图像来提交表单......在这种情况下使用 <input type="image" src="...">

如果您真的想使用锚点,那么您必须使用 javascript:

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

于 2008-11-08T18:59:52.013 回答
9

input type=image将为您完成。

于 2008-11-08T18:59:29.033 回答
7

未经测试/可能会更好:

<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>
于 2008-11-08T18:59:47.363 回答
7
 <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.
于 2010-04-07T14:38:36.900 回答
2

一个方便的补充是可以从额外的按钮更改发布网址,以便您可以使用不同的按钮发布到不同的网址。这可以通过设置表单的“动作”属性来实现。这是使用 jQuery 时的代码:

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

action-attribute 与旧 jQuery 版本存在一些问题,但在最新版本中你会很高兴。

于 2013-02-26T12:02:31.353 回答
1

像这个页面的东西?

<!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>
于 2008-11-08T19:02:06.033 回答
1

不要忘记“按钮”元素,它可以在里面处理更多的 HTML ......

于 2008-11-10T12:49:00.200 回答
0

我们一直在表单上用这个替换提交按钮:

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

单击图像提交表单。希望有帮助!

于 2013-01-18T22:15:46.020 回答