Javascript 触发器不';无法处理通过ajax加载的内容
我有以下问题。我被迫重写以下所有函数以创建此函数:Javascript 触发器不';无法处理通过ajax加载的内容,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有以下问题。我被迫重写以下所有函数以创建此函数: $(document).on(“change”,type of),function(){ 与此相反: $('type of')。在('change',function(){ 我之所以这样做是因为我在操作DOM,在通过ajax加载内容之后,所有函数都停止了操作。现在它们工作了一半,因为触发器没有完成它们的工作。下面所有的代码都添加到页面index.php。在这个页面中,我有div\35; content,我正在通过ajax重新加载它的内容。以下所
$(document).on(“change”,type of),function(){
与此相反:
$('type of')。在('change',function(){
我之所以这样做是因为我在操作DOM,在通过ajax加载内容之后,所有函数都停止了操作。现在它们工作了一半,因为触发器没有完成它们的工作。下面所有的代码都添加到页面index.php
。在这个页面中,我有div\35; content
,我正在通过ajax重新加载它的内容。以下所有函数都与我的问题是如何为这个函数创建合适的触发器
还有一个问题:在我的例子$(document).ready(function(){
)中,这是正确的语法吗
$(document).ready(function() {
// ALSO SOME CODE HERE (variables and etc.)
$(document).on("change","#type_of",function(){
// FUNCTION CODE
})
$(document).on("change","#order_form",function(){
// FUNCTION CODE
})
$( '#type_of, #orer_form' ).trigger( 'change' );
})
$(document).on("change","input[name=window-type]",function(){
// FUNCTION CODE
}
$(document).on("change","#shutter_type",function(){
// FUNCTION CODE
})
$( '#shutter_type, #input[name=window-type]' ).trigger( 'change' );
这里也是我的一个ajax调用,我使用它来重新加载div\content
function displayPhoto(photoid){
$.ajax({
url: "includes/displayphoto.php?photo_id="+photoid
}).done(function(data) {
if (data == false)
{
alert ("ERROR");
}
else
{
$('#content').html(data);
}
});
}
$(document).on("click",".photo-id",function(){
var photoid = $(this).data("photoid");
displayPhoto(photoid);
});
您正在尝试对没有任何直接事件处理程序的元素执行
.trigger()
事件。由于您使用的是事件委派,因此必须执行.trigger()
元素本身上的事件,在本例中是id为#type_的元素
,#order_form
,以及在文档节点上处理事件的所有其他元素
试试这个主意,我希望这对你有帮助
$(document).on(“更改”上的“#选择1”,函数(){
//这里的ajax请求可以传递给params
$(“#test”).trigger('click',[$(this.val());
});
$(文档)。在(“单击”,“测试”,函数(事件,参数1){
//这里是ajax请求
$(“#结果”).html(参数1);
});
1.
2.
发生这种情况是因为新元素与任何事件都不相关。因为它们只是在加载
一个快速修复方法是在加载内容后立即在else上触发document.ready
else
{
$('#content').html(data);
$(document).trigger('ready');
}
更好的解决方案是创建一个函数,比如说“bind”
谢谢大家的关注 在你的帮助下,我终于找到了解决办法 根据@peterpeterson和@jAndy的建议,我想出了一个好主意。我将
$(document).ready(function(){
替换为function readydoc()
,我在html(data)
之后调用它
然后我将触发器函数放在函数readydoc()的close标记前面
它的工作非常完美。谢谢。不幸的是,仍然没有任何结果。据我所知,您正在尝试在稍后添加到DOM中的元素上触发事件(因此在您希望手动触发事件时,在DOM中不是这样)。那么你希望它做什么呢???为什么不使用引用函数作为处理程序?@jAndy我猜OP调用触发器时元素不在DOM中。然后我没有真正理解我用“更好”解决方案尝试过的逻辑,但它仍然不会触发函数。这是可行的,因为如果我不添加bind()除此之外,我甚至不能手动调用我的函数。无论如何,它仍然不能触发函数。
else
{
$('#content').html(data);
$(document).trigger('ready');
}
function bind(){
//All on... must go here.
$(document).off("change","input[name=window-type]");
$(document).on("change","input[name=window-type]",function(){
// FUNCTION CODE
}
//and on every time you load new data call it:
else
{
$('#content').html(data);
bind();
}
function displayPhoto(photoid){
$.ajax({
url: "includes/displayphoto.php?photo_id="+photoid
}).done(function(data) {
if (data == false)
{
alert ("ERROR.");
}
else
{
$('#content').html(data);
readydoc();
}
});
}
function readydoc ()
// ALSO SOME CODE HERE (variables and etc.)
$(document).on("change","#type_of",function(){
// FUNCTION CODE
})
$(document).on("change","#order_form",function(){
// FUNCTION CODE
})
$( '#type_of, #orer_form, #shutter_type, input[name=window-type]' ).trigger( 'change' );
}
$(document).on("change","input[name=window-type]",function(){
// FUNCTION CODE
}
$(document).on("change","#shutter_type",function(){
// FUNCTION CODE
})