JQuery 函数在初始触发时不起作用
JQuery function not working on initial trigger
所以我有一个触发jquery函数的按钮。在此函数中,进行 AJAX 调用(取决于某些因素),将div 'XYZ' 附加到文档中的某个位置。完成此操作后,我希望div XYZ弹出具有leanModal bootstrap特征。
这是触发的函数:
$(document).on("click", ".view_order", function(){
//…stuff...
var filled = $(this).attr("data-filled");
if(filled == 0){
$.ajax({
type: 'post',
data: {action: 'ABC123', var1: var1},
dataType: 'json',
success: function(popup){
//div XYZ is created and appended.
}
})
}
// Now I call a function that mimics the bootstrap leanModal function (tailored to work
// without a selector)
$.leanModalNoSelector({
top : 200,
overlay : 0.6,
closeButton : ".modal_close"
}, $(this));
});
下面是一个来自leanModalNoSelector的代码的缩写区域:
var defaults={
. . . . . . . . etc. . . . . .
};
// this creates the grey background overlay:
var overlay=. . . . . ;
$("body").append(overlay);
options=$.extend(defaults,options);
return this.each(function(){
var o=options;
$(this).click(function(e){
// Getting the href attribute of the original, clicked element (which points to div XYZ)
var modal_id=$(this).attr("href");
$("#lean_overlay").click(function(){
close_modal(modal_id)
});
$(o.closeButton).click(function(){
close_modal(modal_id)
});
// dimensions of the div XYZ set here
. . . . .etc. . . . . . .
$("#lean_overlay").css({
//dealing with the overlay some more….
});
$("#lean_overlay").fadeTo(200,o.overlay);
$(modal_id).css({
"display":"block","position":"fixed",.. . . . . . .etc. . . .
});
$(modal_id).fadeTo(200,1);e.preventDefault()
})
});
function close_modal(modal_id){
… … … … … … …
})
}
}
我的问题是这完美地工作 - 第二次点击它。第一次单击触发器元素时,将创建新的div,但 leanModal 函数根本不执行任何操作。但是,如果我第二次单击它,leanModal 函数可以正常工作。
我的问题是为什么它在第一次点击后不起作用......感谢提前提供的任何帮助,伙计们。
因为您的模态函数在 ajax 成功回调之前调用。您有两种选择来解决此问题:
-
在附加div xyx 后,从 ajax 成功回调内部调用模型函数。
-
将 ajax 配置异步属性设置为 false,默认情况下为 true。
例:
$.ajax({ 类型:"帖子", data: {action: 'ABC123', var1: var1}, 数据类型:"json", 异步:假, 成功:函数(弹出窗口){ 创建并附加div XYZ。 } })
在 ajax 上添加异步参数并将其设置为 false:
$(document).on("click", ".view_order", function(){
//…stuff...
var filled = $(this).attr("data-filled");
if(filled == 0){
$.ajax({
type: 'post',
data: {action: 'ABC123', var1: var1},
dataType: 'json',
async: false,
success: function(popup){
//div XYZ is created and appended.
}
})
}
// Now I call a function that mimics the bootstrap leanModal function (tailored to work
// without a selector)
$.leanModalNoSelector({
top : 200,
overlay : 0.6,
closeButton : ".modal_close"
}, $(this));
相关文章:
- 监视函数从服务返回不起作用,但作用域函数起作用
- Ember Data DS.Model's set函数不起作用
- Javascript:If-then语句在函数中不起作用
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- AngularJS指令部分应用的函数don'不起作用
- 为什么我的JavaScript堆栈排序函数不起作用
- 嵌套到另一个函数中的Fancybox函数;不起作用
- Javascript onchange()函数不起作用
- I'我试图在文本区域中进行特定的输入,调用特定的javascript函数,但没有成功;不起作用
- javascript函数调用不起作用
- 另一个Ajax函数触发的Ajax函数不起作用
- 从PHP调用JS函数不起作用
- 清除函数中if语句内部不起作用的间隔
- js函数堆栈传入变量,.hide()不起作用
- jQuery validate函数不起作用
- jQuery克隆函数在chrome中不起作用
- JS-窗口宽度函数不起作用
- 函数调用不起作用
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- javascript函数,该函数不起作用,但不会显示任何错误