繁体   English   中英

为什么jQuery只在内部加载函数中改变一次?

[英]Why does jQuery change only fires once inside load function?

我有一个div加载一个表单,我想在它更改时加载带有数据的表单url,但它只触发一次

我的代码

$( "#calculator-wrapper" ).load( "mysite.com/new2/kalkulator/index.php", function(data) {
    $('#calculator').change(function(){
        var formData = $(this).serialize();    
        $("#calculator-wrapper").load("mysite.com/new2/kalkulator/index.php?"+formData+'&sub=Sz%C3%A1mol');
    });    
});

它工作,但只有一次,我是一个初学者,所以我卡住了一秒钟,我试图使用bind()on()但这也不起作用。

任何人都可以给我一个提示吗?

尝试

$(document).on("change","#calculator",function(){
    var formData = $(this).serialize();    
    $("#calculator-wrapper").load("mysite.com/new2/kalkulator/index.php?"+formData+'&sub=Sz%C3%A1mol');
}); 

因为您需要动态创建对象的委派。

你需要事件委托来改变事件。这个:

$( "#calculator-wrapper" ).load( "mysite.com/new2/kalkulator/index.php", function(data) {
$(document).on("change",'#calculator',function(){
    var formData = $(this).serialize();
    $( "#calculator-wrapper" ).load("mysite.com/new2/kalkulator/index.php?"+formData+'&sub=Sz%C3%A1mol');
});});

暂无
暂无

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

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