我有一个JQuery函数,试图改变元素的id,名称和类.id和类更改似乎有效,但出于一些奇怪的原因,尝试更改元素的名称永远不会起作用.
$(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));
});
}
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
kar*_*m79 55
将name不能因为更换一次修改了id,在随后的表情选择器(使用未经修改的 ID)是选择什么:)
$("#" + id).attr('id', 'sel' + rowIndex);
$("#" + id).attr('name', 'sel' + rowIndex); // this can't ever work
Run Code Online (Sandbox Code Playgroud)
尝试将它们链接在一起,以保持对当前选择的引用:
$("#" + id).attr('id', 'sel' + rowIndex)
.attr('name', 'sel' + rowIndex);
Run Code Online (Sandbox Code Playgroud)
或者,重新排序语句,以便在更改ID 之前更改名称(和/或其他任何内容):
$("#" + id).attr('name', 'sel' + rowIndex);
$("#" + id).attr('id', 'sel' + rowIndex);
Run Code Online (Sandbox Code Playgroud)
您还可以将选择分配给变量:
var $el = $("#" + id);
$el.attr("id", 'sel' + rowIndex);
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86269 次 |
| 最近记录: |