使用 jQuery 更改名称属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2715260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing name attribute using jQuery
提问by kevin
I've got a jQuery function that attempts to change the id
, name
and class
attribute values of an element.
我有一个 jQuery 函数,它尝试更改元素的id
,name
和class
属性值。
The id
and class
change seems to work but for some curious reason, trying to change the name
of the element never works.
该id
和class
变化,似乎工作,但由于某种原因感到好奇,试图改变name
的元素永远不会奏效。
Here is my code:
这是我的代码:
$(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));
});
}
});
}
});
});
回答by karim79
The name
cannot be changed because once you have modified the id
, the selector in the subsequent expression (which uses the unmodifiedid) is selecting nothing :)
在name
无法改变,因为一旦你修改了id
,在随后的表情选择器(使用未经修改的ID)是选择什么:)
$("#" + id).attr('id', 'sel' + rowIndex);
$("#" + id).attr('name', 'sel' + rowIndex); // this can't ever work
Try chaining them together like this, to keep the reference to the current selection:
尝试像这样将它们链接在一起,以保持对当前选择的引用:
$("#" + id).attr('id', 'sel' + rowIndex)
.attr('name', 'sel' + rowIndex);
Alternatively, reorder the statements such that you change the name (and/or whatever else) beforechanging the id:
或者,重新排序语句,以便在更改 id之前更改名称(和/或其他任何内容):
$("#" + id).attr('name', 'sel' + rowIndex);
$("#" + id).attr('id', 'sel' + rowIndex);
You can also assign the selection to a variable:
您还可以将选择分配给变量:
var $el = $("#" + id);
$el.attr("id", 'sel' + rowIndex);
...
回答by Rabbott
Karim is right,
卡里姆说得对
$("#" + id).attr('classname', 'selected');
$("#" + id).attr('id', 'sel' + rowIndex);
$("#" + id).attr('name', 'sel' + rowIndex);
could be changed to
可以改为
$("#" + id).attr('name', 'sel' + rowIndex);
$("#" + id).attr('classname', 'selected');
$("#" + id).attr('id', 'sel' + rowIndex);
to change the name first, $("#" + id) is the same as getElementById and once you change the id, its no longer the element you meant to refer to
首先更改名称,$("#" + id) 与 getElementById 相同,一旦更改 id,它就不再是您要引用的元素
回答by user3152459
Instead of chaining you can pass into .attr()
您可以传入而不是链接 .attr()
{
"id" : "sel" + rowIndex ,
"name" : "sel" + rowIndex
}
A lot of jQuery functions accept objects like above when you have to pass in (string comma string) data like .css()
and .animate()
当您必须传入(字符串逗号字符串)数据时,许多 jQuery 函数都接受像上面这样的对象.css()
和.animate()