2010-04-29 43 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