【发布时间】:2021-01-12 16:12:35
【问题描述】:
我有一个 jQuery 函数,它尝试更改元素的 id、name 和 class 属性值。
id 和 class 更改似乎有效,但出于某种奇怪的原因,尝试更改元素的 name 始终无效。
这是我的代码:
$(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) {
$("#" + id).attr('classname', 'selected');
$("#" + id).attr('id', 'sel' + rowIndex);
$("#" + id).attr('name', 'sel' + rowIndex); // this never works
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);
$.each(data, function (GetSubCatergories, Category) {
$('#parent_id').append($("<option></option>").
attr("value", Category.category_id).
text(Category.name));
});
}
});
}
});
});
【问题讨论】:
标签: jquery