Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更改与其父div索引号相关的输入名称_Javascript_Jquery - Fatal编程技术网

Javascript 如何更改与其父div索引号相关的输入名称

Javascript 如何更改与其父div索引号相关的输入名称,javascript,jquery,Javascript,Jquery,我有多个表单输入,它们在名称中使用数组。用户可以复制该表单。当表单被复制时,输入字段的名称相应地改变。基本上这就是我的状态 <div class="machine-stations"> <div class="property-container"> <input name="machine[process][0][system][100][station][52][name]" /> <input name="machi

我有多个表单输入,它们在名称中使用数组。用户可以复制该表单。当表单被复制时,输入字段的名称相应地改变。基本上这就是我的状态

<div class="machine-stations">
    <div class="property-container">
      <input name="machine[process][0][system][100][station][52][name]" />
      <input name="machine[process][0][system][100][station][52][price]" />
      <!-- more input fields -->
    </div>
</div>
<div class="machine-stations">
    <div class="property-container">
      <input name="machine[process][1][system][100][station][60][name]" />
      <input name="machine[process][1][system][100][station][52][price]" />
      <!-- more input fields -->
    </div>
</div>
<div class="machine-stations">
    <div class="property-container">
       <input name="machine[process][2][system][100][station][40][name]" />
       <input name="machine[process][2][system][100][station][52][price]" />
       <!-- more input fields -->
    </div>
</div>


我试图实现的是,我必须根据
.machine stations
索引编号更改
[process]
之后的编号。用户可以复制
。机器站
。如果输入字段包含在
.machine stations
索引0中,则
[process]
后面的数字应为0,依此类推。谢谢

我相信这就是你想要的:

在fiddle中,我更改了html中的数字,以便可以将它们更改回0,1,2

既然你说以上不是你要找的,而且还没有澄清,我再猜一次。。。使用更新的html结构。这将使每个
机器站内的索引重新启动为0。机器站

还有第三个猜测。。。在这种情况下,第一个
机器站中的所有内容都将为0,第二个
机器站中的所有内容都将为1,以此类推


您可以简单地使用以下方法进行操作

第一

代码一开始很简单,小函数也变得很有趣
我在编辑之前没有承认这个问题,但检查了一下小提琴。我相信这正是您的发展方向。

非常感谢,但这不是我想要的。这可能是
this.name=this.name.replace(…)
,这样您就不用在每次迭代中创建2个不必要的jQuery对象和两个方法调用了。@u54r,那么您到底在寻找什么呢?也许你应该解释一下你希望发生什么,你希望结果是什么。
var $inputs = $(".machine-stations").find("input");
$inputs.each( function(i) {
    this.name = this.name.replace(/\[process\]\[\d+\]/,"[process]["+i+"]");
});
$(".machine-stations").each( function() {
    $(this).find("input").each( function(i) {
        this.name = this.name.replace(/\[process\]\[\d+\]/,"[process]["+i+"]");
    });
});
$(".machine-stations").each( function(i) {
    $(this).find("input").each( function() {
        this.name = this.name.replace(/\[process\]\[\d+\]/,"[process]["+i+"]");
    });
});
$('.machine-stations input').attr('name',function(i,v){
    return v.replace(/process\]\[\d+/,'process]['+i);
    // or return v.replace(/(process\]\[)\d+/,'$1'+i);
});