Javascript 如何在提交时重新加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33483960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:07:48  来源:igfitidea点击:

How to reload a page on Submit

javascriptjqueryhtmlformstwitter-bootstrap

提问by Lara

I have a Bootstrap Modal which contains a form. The Modal also contains a Submit and Cancel Button. The cancel button is working fine and it is closing the Modal successfully. Now as per my requirement on Submit Button Click of the Modal the Form is Submitting Successfully by passing the User inputs to Web Service but Modal is not getting closed. Also I need to reload the page on Submit button click event only. Here is my HTML..

我有一个包含表单的 Bootstrap Modal。模态还包含提交和取消按钮。取消按钮工作正常,并且成功关闭了 Modal。现在,根据我对模态的提交按钮单击的要求,通过将用户输入传递给 Web 服务,表单正在成功提交,但模态没有关闭。此外,我只需要在提交按钮单击事件上重新加载页面。这是我的 HTML ..

<div class="modal fade" id="StudentModal" tabindex="-1" role="dialog" aria-labelledby="StudentModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog">
  <div class="modal-content">
     <form action="~/GetStudent" class="form-horizontal" role="form" method="post" id="frmStudent">
        <div class="modal-footer">
           <div class="pull-right">
              <button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-ok"></i> Save</button>
              <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
           </div>
        </div>
     </form>
  </div>

I tried following ways to close the Modal ..

我尝试了以下方法来关闭 Modal ..

$('#frmStudent').submit(function() {
$('#StudentModal').modal('hide');
return false;
});

As per my Requirement I need to close the Modal on Submit event and reload the page after getting back from Web Service. How can I do this with Jquery?

根据我的要求,我需要在提交事件时关闭模态并在从 Web 服务返回后重新加载页面。我怎样才能用 Jquery 做到这一点?

Note: I am not supposed to use Ajax Call here ..I need to submit form from form action only

注意:我不应该在这里使用 Ajax Call ..我只需要从表单操作提交表单

回答by adilbo

This HTML will not work:

此 HTML 将不起作用:

<form onsubmit="window.location.reload();">

But this HTML does the job:

但是这个 HTML 可以完成这项工作:

<form onsubmit="setTimeout(function(){window.location.reload();},10);">

So the browser has enough time to submit the form and then reload the page ;-)

所以浏览器有足够的时间提交表单然后重新加载页面;-)

回答by Ayo K

after submitting your form you can just make this call via javascript:

提交表单后,您可以通过 javascript 进行此调用:

window.location.reload();

回答by Bogdan M.

If you want the page to reload when you submit a form, use the onsubmitevent handler:

如果您希望在提交表单时重新加载页面,请使用onsubmit事件处理程序:

document.getElementById("frmStudent").onsubmit = function(){
    location.reload(true);
}

This code should reload the page, submit your form, and close the modal.

此代码应重新加载页面,提交表单并关闭模式。