Javascript <a href> 而不是 <input submit> 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2850358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:10:15  来源:igfitidea点击:

<a href> instead of <input submit> button

javascripthtml

提问by Kukoy

I originally had a submit button.

我最初有一个提交按钮。

<input class="submit" type="submit" class="input" value="Add" name="command" />

but now I would like to use a <a href>instead. The issue is, the value="Add"is very important.

但现在我想用 a<a href>代替。问题是,这value="Add"非常重要。

I'm currently making the <a href>like this.

我目前正在制作<a href>这样的。

<a href="javascript:document.register.submit();">submit</a>

Any suggestions? Issue is, the site is not picking up that this specific <a href>was clicked, and therefore won't run my php code.

有什么建议?问题是,该站点没有发现这个特定的<a href>被点击了,因此不会运行我的 php 代码。

回答by RoToRa

As Pekka suggests: Don't do this. It will make your form unnecessarily be dependent on JavaScript!

正如 Pekka 建议的那样:不要这样做。它会让你的表单不必要地依赖 JavaScript!

The best thing to do is use CSS to style the button to look like a link:

最好的办法是使用 CSS 将按钮的样式设置为看起来像一个链接:

input.submit {
  margin: 0;
  padding: 0;
  border: none;
  color: blue;
  background-color: transparent;
  text-decoration: underline;
}

回答by nickf

Add a hidden field, and an extra line into the javascript:

在 javascript 中添加一个隐藏字段和一个额外的行:

<input type="hidden" name="command" />
<a href="javascript: document.register.command.value='Add'; document.register.submit();">
    Submit
</a>

That said, you might be better off using CSS to style the submit button to look how you want it to.

也就是说,您最好使用 CSS 来设置提交按钮的样式以使其看起来像您想要的那样。

回答by bitLost

First off I highly recommend jquery. It makes a world of difference when working with javascript and simplifies a number of issues especially cross different browsers.

首先我强烈推荐jquery。它在使用 javascript 时会产生很大的不同,并简化了许多问题,尤其是跨不同浏览器的问题。

What I suggest is that you create a hidden input to set the value with.

我建议您创建一个隐藏输入来设置值。

<script type="text/javascript">
function submitFormWithValue(val){
  document.getElementById('command').value = val;
  document.forms["test"].submit();
}
</script>

<form id="test" name="test" action="#" method="post">
<input type="hidden" name="command" value="" />
</form>

<a href="javascript:submitFormWithValue('foo')">Submit</a>

I didn't test my code but it should be close enough to see how it works. Again check out jquery it will make dealing with javascript a lot easier!

我没有测试我的代码,但它应该足够接近以了解它是如何工作的。再次检查 jquery 它将使处理 javascript 更容易!