在我提交表单后,下面的行会打开一个弹出模式。
<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成功部分来获得弹出模式。
答案 0 :(得分:0)
js文件中的函数是immediately invoked function IIF
,这就是为什么它自己执行并且你看到弹出的
您需要删除在函数表达式之前和之后删除括号,使其行为与普通函数一样。 您可以将此功能视为
function someFunctionName(){
// you code goes here
}
然后在ajax成功区块中,您可以拨打someFunctionName()