使用函数调用调用 jQuery 弹出窗口


call jquery popup with function call

提交表单后,下面的行会打开一个弹出模式。

<input class="js__p_start" type="submit"  value="submit" name="submit"/>

我不想在提交表单后打开弹出窗口。提交表单后,ajax 用于将数据发布到 php。我想在成功的 ajax 请求后打开弹出窗口。Ajax 代码如下,

<script>
$(document).ready(function(){
$("#hidden_form").submit(function(){
var mobile = $("#mobile").val();
var name = $("#name").val();

var dataString = 'mobile='+ mobile + '&name='+ name;
if(name=='')
{
alert("Please Enter your name");
}
else
{
$.ajax({
type: "POST",
url: "file.php",
data: dataString,
cache: false,
success: function(result){
//I Want to call Popup here, so that popup will appear only after user properly entereed form fields
}
});
}
return false;
});
});
</script>

这是JS文件:

(function($) {
$.fn.simplePopup = function(event) {
var simplePopup = {
  settings: {
    hashtag: "#/",
    url: "popup",
    event: event || "click"
  },
  initialize: function(link) {
    var popup = $(".js__popup");
    var body = $(".js__p_body");
    var close = $(".js__p_close");
    var routePopup = simplePopup.settings.hashtag + simplePopup.settings.url;
    var cssClasses = link[0].className;
    if (cssClasses.indexOf(" ") >= 0) {
      cssClasses = cssClasses.split(" ");
      for (key in cssClasses) {
        if (cssClasses[key].indexOf("js__p_") === 0) {
          cssClasses = cssClasses[key]
        }
      };
    }
    var name = cssClasses.replace("js__p_", "");
    // We redefine the variables if there is an additional popap
    if (name !== "start") {
      name = name.replace("_start", "_popup");
      popup = $(".js__" + name);
      routePopup = simplePopup.settings.hashtag + name;
    };
    link.on(simplePopup.settings.event, function() {
      simplePopup.show(popup, body, routePopup);
      return false;
    });
    $(window).on("load", function() {
      simplePopup.hash(popup, body, routePopup);
    });
    body.on("click", function() {
      simplePopup.hide(popup, body);
    });
    close.on("click", function() {
      simplePopup.hide(popup, body);
      return false;
    });
    $(window).keyup(function(e) {
      if (e.keyCode === 27) {
        simplePopup.hide(popup, body);
      }
    });
  },

  centering: function(popup) {
    var marginLeft = -popup.width()/2;
    return popup.css("margin-left", marginLeft);
  },
  show: function(popup, body, routePopup) {
    simplePopup.centering(popup);
    body.removeClass("js__fadeout");
    popup.removeClass("js__slide_top");
    location.hash = routePopup;
    document.getElementById("menu_but").style.visibility = "hidden";
    document.getElementById("toTop").style.visibility = "hidden";
    document.getElementById("cbp-spmenu-s1").style.visibility = "hidden";
  },
  hide: function(popup, body) {
    popup.addClass("js__slide_top");
    body.addClass("js__fadeout");
    location.hash = simplePopup.settings.hashtag;
    document.getElementById("menu_but").style.visibility = "visible";
    document.getElementById("toTop").style.visibility = "visible";
    document.getElementById("cbp-spmenu-s1").style.visibility = "visible";
  },
  hash: function(popup, body, routePopup) {
    if (location.hash === routePopup) {
      simplePopup.show(popup, body, routePopup);
    }
  }
};

return this.each(function() {
  var link = $(this);
  simplePopup.initialize(link);
});
 };
})(jQuery);

基本上我正在寻找一些函数调用 insidde AJAX 成功部分来获取弹出模式。

js文件中的函数是immediately invoked function IIF这就是为什么它自己执行并且您会看到弹出窗口的原因.

您需要删除函数表达式前后的括号,以使其行为像普通函数一样。您可以将此功能视为

function someFunctionName(){
    // you code goes here
}

然后在 ajax 成功块中,您可以调用someFunctionName()