简体   繁体   English

HTML表单在提交时刷新

[英]HTML form refreshes on submit

I have a javascript function that submits a form. 我有一个提交表单的javascript函数。 I have searched through a lot of questions and found answers, but that does not seem to help. 我搜索了很多问题并找到了答案,但这似乎无济于事。

function setActionAndSubmit(action){
    $('input:hidden[name=action]').val(action);
    $("blueForm").submit();
}

I have seen some answers where people suggested return false; 我看到了一些答案,有人建议return false; But that does not seem to work either. 但这似乎也不起作用。

How do I stop the submit from refreshing the page? 如何阻止提交刷新页面?

If you want to create remote post form, you should use ajax 如果要创建远程发布表单,则应使用ajax

Here is little example how it could look, for simple login form. 这是简单登录表单的小示例。

$.post( "http://127.0.0.1/loginAction",
        { username: "root", password: "toor" },
        function(data) {
          // Data received from php side
        } );

Use jQuery's ajax helpers to submit it instead. 可以使用jQuery的ajax助手来提交它。 The below code will make the same request as submitting a form, except doesn't redirect/refresh the page. 以下代码与提交表单的请求相同,只是不重定向/刷新页面。 callback is called with the response. callback与响应一起调用。

function setActionAndSubmit(action, callback) {
  var $form = $("blueForm");
  $form.find('input:hidden[name=action]').val(action);
  $.post($form.attr("action"), $form.serialize(), callback);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM