当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Jquery click trigger doesnt work

问题描述:

I have this function:

$(".action").click(function(){

$.get('suppliers/template/get_options_list.php?action='+action+'&id='+id+'&optvalue='+optvalue+'&pid='+pid+'',

update_options);

}

});

function update_options(options){

$('#selectedoptions').html(options);

}

It works fine for the first click trigger but after ajax returns results next click doesnt fire.. No errors, console is clear but stilll

What could cause this? Thanks

网友答案:
$(document).on('click','.action',function(){
$.get('suppliers/template/get_options_list.php?action='+action+'&id='+id+'&optvalue='+optvalue+'&pid='+pid+'',
update_options);      
});    
function update_options(options){
$('#selectedoptions').html(options);
}

and if .action if a submit input use

$(document).on('submit','your_form_ID_or_Class',function()
网友答案:

Try binding the click event to the body, rather than the specific element, which won't be bound anymore once you replace the html:

    $("body").on("click", ".action", function(){            
      $.get('suppliers/template/get_options_list.php?action='+action+'&id='+id+'&optvalue='+optvalue+'&pid='+pid+'',
update_options);        
    }    
});    
function update_options(options){
$('#selectedoptions').html(options);    
}       
网友答案:

I removed the ajax returned html and instead of it on ajax response I used .remove() function to remove elements instead of generating new html.. Thank you for your help guys ! This isnt the original solution but it works fine..

分享给朋友:
您可能感兴趣的文章:
随机阅读: