jQuery怀疑命名约定问题

jQuery怀疑命名约定问题,jquery,Jquery,我对这个jQuery函数有一个问题,函数中重命名下拉列表id、类和名称的部分只对第一个下拉列表有效,后续的部分不起作用,有什么想法吗 我怀疑它可能与cat.parent_id中的命名约定有关,但它是asp.net mvc模型绑定所必需的 $(document).ready(function () { $("table select").live("change", function () { var id = $(this).attr('id');

我对这个jQuery函数有一个问题,函数中重命名下拉列表id、类和名称的部分只对第一个下拉列表有效,后续的部分不起作用,有什么想法吗

我怀疑它可能与cat.parent_id中的命名约定有关,但它是asp.net mvc模型绑定所必需的

$(document).ready(function () {

    $("table select").live("change", function () {

        var id = $(this).attr('id');

        if ($(this).attr('classname') != "selected") {

            var rowIndex = $(this).closest('tr').prevAll().length;
            $.getJSON("/Category/GetSubCategories/" + $(this).val(), function (data) {
                if (data.length > 0) {

                    //problematic portion
                    $("#" + id).attr('classname', 'selected');
                    $("#" + id).attr('name', 'sel' + rowIndex);
                    $("#" + id).attr('id', 'sel' + rowIndex);

                    var position = ($('table').get(0));

                    var tr = position.insertRow(rowIndex + 1);
                    var td1 = tr.insertCell(-1);
                    var td2 = tr.insertCell(-1);
                    td1.appendChild(document.createTextNode('SubCategory'));
                    var sel = document.createElement("select");
                    sel.name = 'parent_id';

                    sel.id = 'parent_id';

                    sel.setAttribute('class', 'unselected');
                    td2.appendChild(sel);
                    $('#parent_id').append($("<option></option>").attr("value", "-1").text("-please select item-"));

                    $.each(data, function (GetSubCatergories, Category) {
                        $('#parent_id').append($("<option></option>").
                                attr("value", Category.category_id).
                                text(Category.name));
                     });

                   sel.name = 'cat.parent_id';

                   sel.id = 'cat.parent_id';
                }

            });

        }
    });
}); 
$(文档).ready(函数(){
$(“表选择”).live(“更改”,函数(){
var id=$(this.attr('id');
if($(this.attr('classname')!=“选中”){
var rowIndex=$(this).closest('tr').prevAll().length;
$.getJSON(“/Category/GetSubCategories/”+$(this).val(),函数(数据){
如果(data.length>0){
//有问题的部分
$(“#”+id).attr('classname','selected');
$(“#”+id).attr('name','sel'+rowIndex);
$(“#”+id).attr('id','sel'+rowIndex);
var位置=($('table').get(0));
var tr=position.insertRow(行索引+1);
var td1=tr.insertCell(-1);
var td2=tr.insertCell(-1);
td1.appendChild(document.createTextNode('SubCategory');
var sel=document.createElement(“选择”);
sel.name='parent_id';
sel.id='parent_id';
sel.setAttribute('class','unselected');
td2.附肢儿童(sel);
$(“#父项id”)。追加($(“”)attr(“值”,“1”)。文本(“-请选择项-”);
$.each(数据、函数(获取子类别、类别){
$('#父项id')。追加($(“”)。
属性(“值”,类别。类别\u id)。
文本(类别名称);
});
sel.name='cat.parent_id';
sel.id='cat.parent_id';
}
});
}
});
}); 

您正在尝试设置您通过Id选择的某个对象的Id,但这对于一开始来说并不好

$("#" + id).attr('id', 'sel' + rowIndex); // Shouldn't do that I think
我想你要换这条线

var id = $(this).attr('id');

然后,当您想在getJSON中访问它时,请执行以下操作:

$(currentDropdown)
因此,您的问题部分现在看起来像:

$(currentDropdown).attr('classname', 'selected');
$(currentDropdown).attr('name', 'sel' + rowIndex);
$(currentDropdown).attr('id', 'sel' + rowIndex);

您是否多次使用了
id=“parent\u id”
?不,我没有,我正在重命名controlsd,因此只有1将使用cat.parent.id作为id和名称
$(currentDropdown).attr('classname', 'selected');
$(currentDropdown).attr('name', 'sel' + rowIndex);
$(currentDropdown).attr('id', 'sel' + rowIndex);