a tag as a submit button?

2019-02-01 18:28发布

Hi I am trying to get "a" tag as a submit button. I found a code somewhere in the web. But it didn't work.

<a href="#" onclick="this.form.submit()">Submit</a>

Is there any code for to achieve my need?

标签: html button tags
5条回答
劫难
2楼-- · 2019-02-01 18:53

Supposing the form is the direct parent you can do:

<a href='#' onclick='this.parentNode.submit(); return false;'>submit</a>

If not you can access through the forms name attribute like this:

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

See both examples here: http://jsfiddle.net/WEZDC/1/

查看更多
女痞
3楼-- · 2019-02-01 18:54

Try this code:

<form id="myform">
  <!-- form elements -->
  <a href="#" onclick="document.getElementById('myform').submit()">Submit</a>
</form>

But users with disabled JavaScript won't be able to submit the form, so you could add the following code:

<noscript>
  <input type="submit" value="Submit form!" />
</noscript>
查看更多
你好瞎i
4楼-- · 2019-02-01 19:00

Try something like below

 <a href="#" onclick="this.forms['formName'].submit()">Submit</a> 
查看更多
Juvenile、少年°
5楼-- · 2019-02-01 19:03

Give the form an id, and then:

document.getElementById("yourFormId").submit();

Best practice would probably be to give your link an id too, and get rid of the event handler:

document.getElementById("yourLinkId").onclick = function() {
    document.getElementById("yourFormId").submit();
}
查看更多
仙女界的扛把子
6楼-- · 2019-02-01 19:03

in my opinion the easiest way would be somthing like this:

<?php>
echo '<a href="link.php?submit='.$value.'">Submit</a>';
</?>

within the "link.php" you can request the value like this:

$_REQUEST['submit']
查看更多
登录 后发表回答