简体   繁体   English

使jQuery函数与Ajax调用一起使用

[英]Making jQuery function work with ajax call

I am trying to achieve something when i click the button. 单击按钮时,我正在尝试实现某些目标。 The button is loaded from the server using an AJAX call. 该按钮是使用AJAX调用从服务器加载的。 However, nothing happens when the button is clicked. 但是,单击按钮时什么也没有发生。 Here is my code: 这是我的代码:

(This ajax call is in JS Fiddle -- only for testing purposes) JS Fiddle (此ajax调用位于JS Fiddle中-仅用于测试目的) JS Fiddle

Code: 码:

<div id="target"></div>

new Request.HTML({
    url: '/echo/html/',
    data: {
        html: "<div id='0001'>"+"<h5 class='title'>Hello World</h5>"+"<h4 class='date'>2014-07-19</h4>"+"<button> Add to Calendar </button>"+"</div>",
        delay: 0
    },
    method: 'post',
    update: 'target'
}).send();

$("button").click(function(){
    var value = $(this).siblings(".title").text();
    value += $(this).siblings(".date").text();
    alert(value);
});

Since the button does not yet exist you can use jQuery.on to delegate an event handler. 由于按钮尚不存在,您可以使用jQuery.on委托事件处理程序。

$("body").on('click', 'button', function(){
  var value = $(this).siblings(".title").text();
  value += $(this).siblings(".date").text();
  alert(value); 
});

Also your fiddle does not work since you are using both jQuery and MooTools but only loading MooTools. 另外,由于您同时使用jQuery和MooTools,而仅加载MooTools,因此您的提琴不起作用。


Added after seeing authors page: 在看到作者页面后添加:

The var value = $(this).siblings(".title").text(); var value = $(this).siblings(".title").text(); selector will not work. 选择器将不起作用。 I would recommend adding an event class (as in calendar event) to the wrapper and using: 我建议将event类(如日历事件中)添加到包装器并使用:

var value = $(this).parents(".event").find(".title").text();

If you are adding elements to the DOM via AJAX (or any other method) and you want listeners to remain bound to them, then you need to attach those listeners to elements in the document that don't change - like $(document) . 如果要通过AJAX(或任何其他方法)将元素添加到DOM,并且希望侦听器保持绑定到它们,则需要将这些侦听器附加到文档中不变的元素上,例如$(document)

eg 例如

$(document).on('click','button',function(e) {
 //your code here
});

The problem with your code is that you're getting all the buttons by doing $("button") and then attaching click listeners to them. 代码的问题在于,通过执行$("button")然后将单击侦听器附加到它们来获取所有按钮。 The problem is that when you're doing this, your button hasn't been attached to the document and, therefore, will not have a click listener attached to it. 问题在于,执行此操作时,您的按钮尚未附加到文档,因此不会在其上附加单击侦听器。

If button doesn't exist on page load. 页面加载时按钮是否不存在。 You can;t use $("button") to address it. 您不能使用$("button")来解决它。 You have to surround it by something else(div for example) and do it like this: $('div').on('click','button', function(){...}) 您必须用其他东西将其包围(例如div),然后像这样: $('div').on('click','button', function(){...})

There are 2 things you need to consider. 您需要考虑两件事。

ONE - jQuery that is included with mootools. 一个-mootools附带的jQuery。 I was surprised that $ function in mootools version that you are using gave me a very BASIC jQuery function. 我很惊讶您正在使用的mootools版本中的$函数给了我一个非常基本的jQuery函数。 I tried higher versions of mootools and still the same happened. 我尝试了更高版本的mootools,但仍然发生了同样的情况。 I am sure it is not jQuery at all and mootools is just using $ variable for their purpose. 我确信它根本不是jQuery,而mootools只是使用$变量来达到目的。 [Optional: So, you may also consider defining jQuery as a different variable in your script.] [可选:因此,您也可以考虑在脚本中将jQuery定义为其他变量。]

So, I added a link to latest jQuery in External Resources on jsfiddle and then it worked. 因此,我在jsfiddle的《外部资源》中添加了指向最新jQuery的链接,然后它起作用了。 Do not forget to expand External Resources on left on jsfiddle. 不要忘记在jsfiddle左侧展开外部资源。 But, if mootools is using $ for its own purpose, then jQuery will surely overwrite it. 但是,如果mootools将$用于其用途,那么jQuery肯定会覆盖它。 Please consider using jQuery as a different variable than $. 请考虑使用jQuery作为不同于$的变量。

TWO - Use the onSuccess function of AJAX request. 二-使用AJAX请求的onSuccess函数。 You call the AJAX request and so you have to wait for some time for browser to load it which depends on user's internet connection. 您调用了AJAX请求,因此必须等待一段时间才能让浏览器加载它,具体取决于用户的Internet连接。 So, this is why we use onError and onSuccess events. 因此,这就是为什么我们使用onError和onSuccess事件。 Write the button function in onSuccess because button does not exist on document until AJAX is loaded. 在onSuccess中编写按钮功能,因为在加载AJAX之前,文档上不存在按钮。

Please check this code and it works. 请检查此代码,它可以工作。

http://jsfiddle.net/U7ewu/ Code: http://jsfiddle.net/U7ewu/代码:

new Request.HTML({
url: '/echo/html/',
data: {
    html: "<div id='0001'>"+"<h5 class='title'>Hello World</h5>"+"<h4 class='date'>2014-07-19</h4>"+"<button> Add to Calendar </button>"+"</div>",
    delay: 0
},
method: 'post',
update: 'target',
onSuccess: function() {
    $("button").click(function(){
        var value = $(this).siblings(".title").text();
        value += $(this).siblings(".date").text();
        alert(value);
    });
}
}).send();

EDIT: 编辑:

Please use $.noConflict(); 请使用$ .noConflict(); so that mootools and jquery do not conflict each other on $ variable. 这样,mootools和jquery就不会在$变量上发生冲突。 $ is already being used by mootols, so please use the word jQuery instead. $已被Mootols使用,因此请改用jQuery。

Please check this jsfiddle. 请检查此jsfiddle。 http://jsfiddle.net/wkMep/ http://jsfiddle.net/wkMep/

Code: 码:

$.noConflict();

new Request.HTML({
url: '/echo/html/',
data: {
    html: "<div id='0001'>"+"<h5 class='title'>Hello World</h5>"+"<h4 class='date'>2014-07-19</h4>"+"<button> Add to Calendar </button>"+"</div>",
    delay: 0
},
method: 'post',
update: 'target',
onSuccess: function() {
    jQuery("button").click(function(){
        var value = jQuery(this).siblings(".title").text();
        value += jQuery(this).siblings(".date").text();
        alert(value);
    });
}
}).send();

try this 尝试这个

$("button").on("click",function(){
    var value = $(this).siblings(".title").text();
    value += $(this).siblings(".date").text();
    alert(value);
});

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

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