2010-04-29 40 views
2

我遇到了这个jQuery函数的问题,重命名id,类和下拉列表名称的功能部分只适用于第一个下拉菜单,后续的不工作,任何想法?jQuery怀疑命名约定问题

我怀疑它可能与命名约定有关,如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'; 
       } 

      }); 

     } 
    }); 
}); 
+0

你是否多次使用id =“parent_id”? – 2010-04-29 11:37:49

+0

不我不是,我重命名控件,所以只有1将有cat.parent.id作为ID和名称 – user327087 2010-04-29 11:39:27

回答

1

你想设置你的ID选择的东西,不能很好的一个开始的ID。

$("#" + id).attr('id', 'sel' + rowIndex); // Shouldn't do that I think 

我想你想的时候,你要访问它里面的getJSON做到这一点与

var currentDropdown = this; 

然后替换该行

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

$(currentDropdown) 

所以你有问题的部分不会看起来像:

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

感谢罗布工作 – user327087 2010-04-29 11:45:54