Jquery选择更改不触发

我需要捕获当一个选择框更改时,应该很简单!
$('#multiid').change(function(){
    alert('Change Happened');
});

但它不工作,我怀疑的问题是,选择框不存在的文档准备好它只创建,如果需要,所以我在HTML中创建它为空,填充它与代码作为测试,但没有工作。

function buildmulti(id,name,price) {
    // build action items for action bar
    var optlist = $('<select></select>').attr('id','multiid').attr('name','multiid');
    optlist.append('<option value="0">Select Size</option>');
    $.each(option,function(index,val) {
        if(val.prodID *1  == id * 1) {
            v = val.ID;
            fprice = price * 1 + val.pricechange * 1;
            t = name + ' - ' + val.variation +  ' - ' + currency + (fprice).toFixed(2);
            optlist.append('<option value="' + v + '">' + t + '</option>');
        }
    })
    $('#addbasket').append(optlist);
};

它可能是另一个支架的地方,但我找不到它!

解决方法

尝试
$(document).on('change','#multiid',function(){
    alert('Change Happened');
});

由于您的选择框是从代码生成的,因此您必须使用event delegation,其中代替$(document),您可以有最近的父元素。

要么

$(document.body).on('change',function(){
    alert('Change Happened');
});

更新:

Second one works fine,there is another change of selector to make it work.

$('#addbasket').on('change',function(){
    alert('Change Happened');
});

理想情况下,我们应该使用$(“#addbasket”)作为最接近的父元素[如上所述]。

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...