jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Jquery改名称属性大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个JQuery函数尝试更改元素的id,name和class。 id和class的改变似乎是有效的,但是由于某些好奇的原因,尝试改变元素的名称永远不会起作用。
$(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));
                });
            }

        });

    }
});
});

解决方法

名称无法更改,因为一旦修改了id,后续表达式中的选择器(使用未修改的id)就是没有选择:)
$("#" + id).attr('id','sel' + rowIndeX);
$("#" + id).attr('name','sel' + rowIndeX); // this can't ever work

尝试将它们链接在一起,以保持当前选择的参

$("#" + id).attr('id','sel' + rowIndeX)
           .attr('name','sel' + rowIndeX);

或者,重新排序语句,以便在更改id之前更改名称(和/或其他方式):

$("#" + id).attr('name','sel' + rowIndeX);
$("#" + id).attr('id','sel' + rowIndeX);

您还可以将选择分配给变量:

var $el = $("#" + id);
$el.attr("id",'sel' + rowIndeX);
...

大佬总结

以上是大佬教程为你收集整理的Jquery改名称属性全部内容,希望文章能够帮你解决Jquery改名称属性所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。