简体   繁体   English

jQuery动态下拉事件未触发

[英]Jquery dynamic dropdown event not firing

Hi i am trying to create dropdownlists dynamically and populating the contents via an ajax call, this sort of works in the sense that the drop down is created and populated as required but its 'change' event does not fire. 嗨,我正在尝试动态创建下拉列表并通过ajax调用填充内容,这种工作方式是根据需要创建和填充下拉列表,但不会触发“更改”事件。 I am posting the code below, if anyone can spot something obvious, can you please let me know regards 我在下面发布代码,如果有人可以发现明显的问题,请告诉我问候

$(document).ready(function () {

 $("#positions select").change(function (e) {
    alert("in");
    var id = $("#category_id").val();
    $.getJSON("/Category/GetSubCategories/" + id, function (data) {
    if (data.length > 0) {

        alert(data.length);
        var position = document.getElementById('positions');
        var tr = position.insertRow(7);
        var td1 = tr.insertCell(-1);
        var td = tr.insertCell(-1);
        var sel = document.createElement("select");
        sel.name = 'sel';
        sel.id = 'sel';
        sel.setAttribute('class', 'category');

        td.appendChild(sel);
        $.each(data, function (GetSubCatergories, category) {
        $('#sel').append($("<option></option>").
       attr("value", category.category_id).
      text(category.name));

        });
    }
    });
    });
 }); 

If your drop down is dynamically generated then you have to bind the change event using .live() handler. 如果下拉列表是动态生成的,则必须使用.live()处理程序绑定更改事件。 live() attaches a handler to the event for all elements which match the current selector, now or in the future. live()将为当前或将来与当前选择器匹配的所有元素的事件添加处理程序。

$("positions select").live("change", function(){
});

Edit 编辑

To get the id 获取ID

$(this).attr("id");

To get value 获得价值

$(this).val();

To get class name 获取班级名称

$(this).attr("class");

inside the change event handler. 在更改事件处理程序中。

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

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