【问题标题】:Changing name attribute using jQuery使用 jQuery 更改名称属性
【发布时间】: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


    【解决方案1】:

    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 有一个name() 函数?一定是笔误。另外,值得注意的是最好调用一次attr(),然后传入一个包含所有设置的对象。
    • 觉得你可能掌握了一些内幕消息!
    • 当然,伙计们,非常感谢,我想我是时候回家了,然后再问任何愚蠢的问题 :)
    【解决方案2】:

    你可以传递给.attr()而不是链接

    {
        "id" : "sel" + rowIndex ,
        "name" : "sel" + rowIndex
    }
    

    当你必须传入.css() 和.animate() 之类的(字符串逗号字符串)数据时,许多 jQuery 函数都接受上述对象

    【讨论】:

      【解决方案3】:

      卡里姆是对的,

      $("#" + id).attr('classname', 'selected');
      $("#" + id).attr('id', 'sel' + rowIndex);
      $("#" + id).attr('name', 'sel' + rowIndex);
      

      可以改成

      $("#" + id).attr('name', 'sel' + rowIndex);
      $("#" + id).attr('classname', 'selected');
      $("#" + id).attr('id', 'sel' + rowIndex);
      

      要先改名字,$("#" + id) 和getElementById一样,一旦改了id,就不再是你要引用的元素了

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题,需要重新分配这些属性。我设法通过使用本机方法 setAttribute() 简单地实现了这一点。

        $('.check-box-id').each(function (index,element) {
            element.setAttribute("id" , 'ingredients'+index+'.id');
            element.setAttribute("name" , 'ingredients['+index+'].id');
           
        })
        

        【讨论】:

          猜你喜欢
          • 2010-09-23
          • 2021-03-06
          • 1970-01-01
          • 2019-01-16
          • 2012-01-18
          • 2023-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多