简体   繁体   English

重新加载页面后如何隐藏模式并显示另一个模式?

[英]How to hide a modal and show another one after reloading the page?

I have a modal form.我有一个模态形式。 When submitting the form, I want the modal to close, the page to reload, and then the success modal to show up.提交表单时,我希望关闭模式,重新加载页面,然后显示成功模式。

But what happened when I clicked submit was that the success modal shows over the modal form, and then the page reloads with no modal on it.但是当我单击提交时发生的事情是成功模式显示在模式表单上,然后页面重新加载而没有模式。

This is my code:这是我的代码:

$.ajax({
    type: "POST",
    url: "includes/handlers/ajax_submit_profile_post3.php",
    data: $('form.profile_post3').serialize(),
    success: function(msg) {
        $("#post_form3").modal('hide');
        location.reload();
        $("#successModal").modal('show');
    },
    error: function() {
        alert('Failure');
    }
});

location.reload already refresh you page so, the last line in your success callback is not executed anymore (or at least it has no effect on the reloaded page). location.reload已经刷新了您的页面,因此,您的成功回调中的最后一行不再执行(或者至少它对重新加载的页面没有影响)。 So no modal will be shown after reload所以重新加载后不会显示模态

What you could do to archive your goal is refreshing the page with a special parameter and check for that parameter when the page is loading (and then show the modal)您可以做些什么来归档您的目标是使用特殊参数刷新页面并在页面加载时检查该参数(然后显示模式)

So instead of location.reload() do something like ( Edited ):所以代替location.reload()做类似(已编辑)的事情:

window.location.search = updateQueryStringParameter(window.location.search,"success","true")

On the page itself (on load) do something like ( Edited ):在页面本身(加载时)上执行类似(已编辑)的操作:

$(document).ready(function() {
    if(getParameterByName("success",window.location.href) == "true"){
        $("#successModal").modal('show');
    }
});

So the reloaded page will now contain an additional url-parameter and the page itself checks for that and opens the success modal when the param is present所以重新加载的页面现在将包含一个额外的 url 参数,页面本身会检查它并在参数存在时打开成功模式

Edit : The code is now working with all urls (also if there are already url parameters), therefor you also need to add two javascript-helper functions:编辑:代码现在适用于所有 url(如果已经有 url 参数),因此您还需要添加两个 javascript-helper 函数:

//https://stackoverflow.com/a/6021027/1578780
function updateQueryStringParameter(uri, key, value) {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}

//https://stackoverflow.com/a/901144/1578780
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

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

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