admin管理员组

文章数量:1404923

hello i want to know how to submit form using a tag i know it is

<a href="javascript:void()" onclick="document.getElementById('myform').submit();"></a>

but when i try javascript validation on form eg

 <form method="post" onsubmit="valid() name="myform" action="index.php">

then the valid function doesn't work so is there any way to make the function work. I want only a tag to be used as onsubmit.

i used simple alert function for checking the validation but it doesnot worked but when i checked it using input type submit tag then it started working.

hello i want to know how to submit form using a tag i know it is

<a href="javascript:void()" onclick="document.getElementById('myform').submit();"></a>

but when i try javascript validation on form eg

 <form method="post" onsubmit="valid() name="myform" action="index.php">

then the valid function doesn't work so is there any way to make the function work. I want only a tag to be used as onsubmit.

i used simple alert function for checking the validation but it doesnot worked but when i checked it using input type submit tag then it started working.

Share Improve this question edited May 1, 2013 at 21:00 user2340767 asked May 1, 2013 at 20:49 user2340767user2340767 5271 gold badge6 silver badges10 bronze badges 2
  • Please show us the code for valid() – Derek Henderson Commented May 1, 2013 at 20:51
  • function valid() { alert(1); } – user2340767 Commented May 1, 2013 at 20:52
Add a ment  | 

4 Answers 4

Reset to default 2

Your onclick() function is looking for a form with the ID myform. In your example code, your form doesn't have an ID or name.

This code should work:

<a href="javascript:void()" onclick="document.getElementById('myform').submit();"></a>

So long as you include the ID in the form element:

<form method="post" onsubmit="valid()" name="myform" id="myform">

Historically, form submission via submit() JavaScript method does not invoke submit event handler (most likely to prevent infinite recursion).

If you want to call a code that is contained inside onsubmit attribute, you should call it explicitly before submitting form programmatically. For example:

var form = document.querySelector('form'),
    link = document.querySelector('a');

link.onclick = function() {
    alert('Handler attached with JS.');
    form.onsubmit.call(form);
    form.submit();
    return false;
};

The answer is simple - submit() method does not trigger onsubmit event. If you want to validate your code on submit then you have to call valid() function by yourself.

Example:

<a href="javascript:void()" onclick="document.getElementById('myform').onsubmit();"></a>

which will trigger your onsubmit (so it will call valid())

or just:

<a href="javascript:void()" onclick="valid()"></a>

submit() method can be then called from that function after positive validation or whenever you want.

I know that the question is really old. But if someone actually needs a solution
i.e. in case when one wants required fields to be validated by the browser.

Inside of the form create an

<input type="submit" name="submit" />

And the click on the link should actually trigger the click on that submit input:

<a href="javascript:void()" onclick="document.getElementById('myform').submit.click();"></a>

本文标签: javascriptSubmit form using a tag and make onsubmit workStack Overflow