admin管理员组

文章数量:1287884

When defining a <form>, it is possible to set the action tag.

How can I set it to null (#), so that it will not actually change the page?

When defining a <form>, it is possible to set the action tag.

How can I set it to null (#), so that it will not actually change the page?

Share Improve this question edited Apr 28, 2014 at 3:42 danielschemmel 11.1k1 gold badge39 silver badges59 bronze badges asked Jan 19, 2011 at 16:04 norris1023norris1023 6333 gold badges16 silver badges25 bronze badges 2
  • 5 I cannot understand this question. – Josiah Ruddell Commented Jan 19, 2011 at 16:08
  • You're going to have to clarify this because currently it makes no sense. – Pointy Commented Jan 19, 2011 at 16:11
Add a ment  | 

5 Answers 5

Reset to default 4

A bit hard to understand but I think what you want is this:

<form action="#">
 <!-- code here -->
</form>

A <form> tag without an action attribute will send the data to the page it was submitted from.

You only need to use the action attribute if you're sending the form data to a different page.

<form method="GET">
    <input type="text" size="10" name="input" value="default">
    <input type="submit" value="Submit">
</form>

(NOTE: This does not work on the file:/// protocol handler.)

For anyone else reading this, it is usually better to have your input or button object return false; when it's clicked. This will make sure the form doesn't follow an action, and instead stays on the page. It would be similar conceptually to what is being asked, as in it will stay on the current page.

This let's you handle submit options in javascript.

Here's an example of how you would submit an email address to your API route, alerting the user what is returned by the API call:

HTML:

<form>
<input type="text" name="email">
<button onclick="do_something(); return false;"
</form>

JAVASCRIPT:

var do_something = function(){
  $.ajax({
    url: "http://myapi./route",
    type: 'POST',
    data: $('form').serialize(),
    context: document.body
  }).done(function(data) {
    alert(data);
  });
};

You can set form action to null in the following way. This worked for me.

< form action ='' >

Try This

<form method="dialog">
    <input type="text" size="10" name="input" value="default">
    <input type="submit" value="Submit">
</form>

本文标签: javascriptHow do you do the action for a form be nullStack Overflow