简体   繁体   English

如何使用Javascript在输入字段的名称属性中动态设置数组的索引

[英]How to Dynamically set the index of array in the name attribute of the input field using Javascript

How to Dynamically set the index of array in the name attribute of the input field using javascript?如何使用javascript在输入字段的名称属性中动态设置数组的索引?

<input type="text" name="item[][name]">
<input type="text" name="item[][description]">
<input type="text" name="item[][brand]">

If I add thesame set of input fields or duplicate it using javascript the result should be like below:如果我添加相同的输入字段集或使用 javascript 复制它,结果应该如下所示:

<input type="text" name="item[0][name]">
<input type="text" name="item[0][description]">
<input type="text" name="item[0][brand]">

<input type="text" name="item[1][name]">
<input type="text" name="item[1][description]">
<input type="text" name="item[1][brand]">

Possibly you do a wrong thing.可能你做错了事。 If you create this fields with js put index there.如果您使用 js 创建此字段,则将索引放在那里。

Example with plain js.使用普通 js 的示例。

var maxInputNum = 5;

var inputName = document.createElement('input'),
    inputDescription = document.createElement('input'),
    inputBrand = document.createElement('input');

maxInputNum++;
inputName.name = 'item[' + maxInputNum + '][name]';
inputDescription.name = 'item[' + maxInputNum + '][description]';
inputBrand.name = 'item[' + maxInputNum + '][brand]';

form.appendChild(inputName);
form.appendChild(inputDescription);
form.appendChild(inputBrand);

Of course you could use jQuery to simplify example.当然,您可以使用 jQuery 来简化示例。

var maxInputNum = 5;

maxInputNum++;
var $inputName = $('<input type="text" name="item[' + maxInputNum + '][name]">'),
    $inputDescription = $('<input type="text" name="item[' + maxInputNum + '][brand]">'),
    $inputBrand = $('<input type="text" name="item[' + maxInputNum + '][description]">');

$('#form').append($inputName);
$('#form').append($inputDescription);
$('#form').append($inputBrand);

But better solution is using something like angularjs.但更好的解决方案是使用 angularjs 之类的东西。

Here you can update all name attribute that container "[digits]" with its order in the selected array of HTMLElements.在这里,您可以根据所选 HTMLElements 数组中的顺序更新容器“[digits]”的所有名称属性。 you can create a function called update order and call it every time something happened and affect order ... Sort Order in Sortable Elements or Delete Element of the array.您可以创建一个名为 update order 的函数,并在每次发生某些事情并影响 order 时调用它... Sortable Elements 或 Delete Element 数组中的排序顺序。

       $("input").each(function (indexitem, itemdata) {
            var name = $(itemdata).attr("name");
            if (name !== undefined && name !== null) {
                const regex = /\[[\d]+\]/gi;
                var newname = name.replace(regex, `[${index}]`);
                $(itemdata).attr("name", newname);
            }
        });

You can simple remove index numbers instead of sequencing.您可以简单地删除索引号而不是排序。 Browsers still will pass the values as an array while keeping order.浏览器仍然会将值作为数组传递,同时保持顺序。

let allIndexedElements=document.querySelectorAll('[name*="item["]');
for (const el of allIndexedElements) {
    el.name = el.name.replace(/\[\d+\]/ig, '[]');
 }

If you want to make sure the order will be kept, you can increment an index number instead of removing it.如果您想确保保留订单,您可以增加索引号而不是删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM