开发者

Stay in same location on submit the form

When i submitting the form the page will navigate to starting of page.

i want to stay on same l开发者_运维知识库ocation. is there any method to do that?


Yes, instead of using the form submission, send an asynchronous request in JavaScript using XHR. To give a concrete example, suppose you have a simple form that looks like:

<form method="POST" action="/path/to/submit">
  <input id="input_name" name="input_name" type="text" value="value" />
</form>

Using XHR, it might look something like the following (using the Closure library for XHR):

<script>
  goog.require('goog.net.XhrIo');
  var submitCompleted = function() {
     alert('Submitted!');
  };

  var submitAction = function() {
     var value = document.getElementById('input_name').value;
     goog.net.XhrIo.send(
       '/path/to/submit',
        submitCompleted,
       'POST',
       'input_name=' + encodeURIComponent(value));
  };
</script>

I'm assuming that you still keep the same input element as before, but that you define your onsubmit function to invoke submitAction instead of using the default submission behavior of your form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜