Javascript .click()不再工作jquery
在使用Javascript .click()不再工作jquery,javascript,jquery,ajax,Javascript,Jquery,Ajax,在使用$.ajax()之前,我一直在使用这个jQuery并且工作正常: $(document).ready(function(){ var urlSerilize = 'some link'; var appList = $("#applications > li > a"); var appCheck = $('input[type=checkbox][data-level="subchild"]'); var installbtn = $(
$.ajax()之前,我一直在使用这个jQuery代码>并且工作正常:
$(document).ready(function(){
var urlSerilize = 'some link';
var appList = $("#applications > li > a");
var appCheck = $('input[type=checkbox][data-level="subchild"]');
var installbtn = $('#submitbtn');
var form = [];
var checked = [];
//var appList = $(".body-list > ul > li");
//var appCheck = $('input[type=checkbox][data-level="subchild"]');
appList.click(function(){
console.log('here!');
if($(this).children().find("input").is(":checked")){
$(this).children().find("input").prop('checked', false);
$(this).children('form').removeClass('checked');
$(this).removeClass("li-checked");
var rmValue = $(this).children('form').attr('id');
form = jQuery.grep(form, function(value) {
return value != rmValue;
});
}else{
$(this).children().find("input").prop('checked',true);
$(this).addClass("li-checked");
$(this).children('form').addClass('checked');
form.push($(this).children('form').attr('id'));
}
console.log(form);
});
installbtn.on('click', function () {
event.preventDefault();
jQuery.each( form, function( i, val ) {
console.log(val);
var request = $.ajax({
url: urlSerilize,
type: 'GET',
data: $('#'+val).serialize(),
success: function( response ) {
console.log( response );
$('#applications').html();
$('#apps_box').html();
}
});
request.done(function(msg){
console.log('Ajax done: ' + 'Yeah it works!!!');
});
request.fail(function(jqXHR, textStatus){
console.log('failed to install this application: ' + textStatus);
});
});
});
});
但是在我使用了这个ajax
code之后,.click()
jQuery事件不再工作:
$(document).ready(function() {
/* loading apps */
//console.log('active');
var request = $.ajax({
url: 'some link',
type: 'GET',
dataType: 'html',
data: {id: 0},
})
request.done(function(data) {
console.log("success");
$('#applications').empty().append(data);
})
request.fail(function() {
console.log("error");
})
request.always(function() {
console.log("complete");
});
//end loading apps
var showmore = $('.showapps');
showmore.click(function(){
var parent = $(this).parent('.tv_apps');
var displayC = parent.children('.body-list').css('display');
console.log(displayC);
if (displayC=='none') {
parent.children('.body-list').show('400');
$(this).children().find('img').rotate({animateTo: 180});
}else{
parent.children('.body-list').hide('400');
$(this).children().find('img').rotate({animateTo: 0});
};
});
});
起初,我以为这是因为ajax加载,并没有停止,但后来我错了。
我已经尝试了窗口。load=function()在Ajax
完成加载后加载脚本的code>DOM函数也是错误的。
因此,如果有任何解决此问题的方法,
谢谢
这是我希望修复的事件:
appList.click(function(){
console.log('here!');
if($(this).children().find("input").is(":checked")){
$(this).children().find("input").prop('checked', false);
$(this).children('form').removeClass('checked');
$(this).removeClass("li-checked");
var rmValue = $(this).children('form').attr('id');
form = jQuery.grep(form, function(value) {
return value != rmValue;
});
}else{
$(this).children().find("input").prop('checked',true);
$(this).addClass("li-checked");
$(this).children('form').addClass('checked');
form.push($(this).children('form').attr('id'));
}
console.log(form);
});
应该是
$('.showapps').on('click', function(){
或
对于动态添加的内容,您需要将事件绑定到它
更多信息:谢谢大家,我终于找到了解决方案。
这是一个关于DOM
的问题,当我使用jquery的ready
方法时,它加载一个空的ul
(没有内容),因此我第一次得出的结论是正确的,我所做的就是删除ready
,并使用一个简单的函数,其中包含所有。click()
事件,然后在request.done()中调用它代码>。
这就是解决方案:
function loadInstaller(){
var urlSerilize = 'some link';
var appList = $("#applications > li");
var appCheck = $('input[type=checkbox][data-level="subchild"]');
var installbtn = $('#submitbtn');
var form = [];
var checked = [];
//...etc
};
$(document).ready(function() {
/* loading apps */
//console.log('active');
var request = $.ajax({
url: 'some link',
type: 'GET',
dataType: 'html',
data: {id: 0},
})
request.done(function(data) {
console.log("success");
$('#applications').empty().append(data);
loadInstaller();
})
//...etc
});
我希望这个答案能帮助其他人:)而不是$('.showapps')。单击(F)
,使用$(文档)。在('click','.showapps',F)
。这样,您将拥有一个全局事件处理程序,该处理程序还将响应将由AJAX请求添加的新Shopps。该事件的可能副本非常有效,我要修复的是将数据添加到数组的事件,在我将ajax添加到网站之前,它已经工作了。showapps工作得很好不再工作的是appList.click()
$(document).on('click','.showapps', function(){
function loadInstaller(){
var urlSerilize = 'some link';
var appList = $("#applications > li");
var appCheck = $('input[type=checkbox][data-level="subchild"]');
var installbtn = $('#submitbtn');
var form = [];
var checked = [];
//...etc
};
$(document).ready(function() {
/* loading apps */
//console.log('active');
var request = $.ajax({
url: 'some link',
type: 'GET',
dataType: 'html',
data: {id: 0},
})
request.done(function(data) {
console.log("success");
$('#applications').empty().append(data);
loadInstaller();
})
//...etc
});