使用JQUERY更改输入名称 [英] Changing input name using JQUERY

查看:58
本文介绍了使用JQUERY更改输入名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据<li>的列表索引更改所有输入名称,但似乎根本没有更改.

I'm trying to change all the input name based on the list index of <li> but it seems like it's not changing at all.

$('.test').click(function() {
for(var i=0;i<$("li.songs").length;i++)
{
  var ob = $("li.songs").eq(i).clone();
  ob.find('input').each(function()
  {
    this.name = this.name+i;
    alert(this.name); //just checking if does it change
  });      
}
});

现在,警报显示了我想要的正确名称,但是当我检查元素并尝试提交表单并显示所有POST值时,名称上没有任何更改.

Now, the alert displays the right name I want, BUT no changes on the name when I inspect the element AND try to submit the form and display all the POST values.

更改前的预期输出示例:

Example expected output before changing:

<li class="songs"><input type="text" name="song" /></li>
<li class="songs"><input type="text" name="song" /></li>
<li class="songs"><input type="text" name="song" /></li>

更改后:

<li class="songs"><input type="text" name="song1" /></li>
<li class="songs"><input type="text" name="song2" /></li>
<li class="songs"><input type="text" name="song3" /></li>

注意:我不希望输入名为song的输入成为数组.

NOTE: I DO NOT WANT the input named song to be an ARRAY.

推荐答案

您正在克隆对象,因此更改是对副本而不是原始DOM节点进行的.

You are cloning the object, so the change is done to a copy rather than the original DOM node.

不要使用clone(),您会没事的.或执行以下操作:

Don't use clone() and you'll be fine. Or do this:

$("li.songs input").each(function(i) {
  $(this).attr('name', $(this).attr('name') + i);
});

这篇关于使用JQUERY更改输入名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆