简体   繁体   English

如何从多个元素的相同名称获取名称属性的索引。

[英]How to get index of name attribute from same name of multiple elements.

I have some input fields, having same names but I only want to get the value of index of each input like this 我有一些输入字段,具有相同的名称,但是我只想像这样获取每个输入的索引值

<input type="text" name="op_price[20]" value="20.00" size="3"  >
<input type="text" name="op_price[2]" value="13" size="3">
<input type="text" name="op_price[14]" value="12" size="3">

So, for example I only wants to get 20, 2, 14 from op_price name attribute, is their any JS or jquery method to do this 因此,例如,我只想从op_price name属性获取20、2、14,是他们执行此操作的任何JS或jquery方法

This will return the indexes in an array: 这将返回数组中的索引:

var indexes = $('[name^="op_price"]').map(function(){
    return this.name.match(/\d+/);
}).get();

console.log(indexes); // ["20", "2", "14"] 

Use attributeContains selector 使用attributeContains选择器

Ex from docs : 例如:

$( "input[name*='man']" ).val();

if you're not going to change the name of your input fields, may be you can iterate through a loop to get all the values? 如果您不打算更改输入字段的名称,是否可以遍历循环以获取所有值? Try this: 尝试这个:

for(i=0;document.getElementById('op_price['+i+']';i++)
 alert(document.getElementById('op_price['+i+']');

this will give you the value of the ith input field in each iteration of the loop! 这将在循环的每次迭代中为您提供第i个输入字段的值!

var name = $("input:text").attr("name");
name = name.replace("op_price[", "").replace("]", "");
alert(name);

See DEMO 演示

like if you get your name property 就像您拥有姓名财产一样

   var name="op_price[20]"  

name.replace(/op_price\[(\d*)?]/gi, $1);

then op_price[20] replace with 20 然后op_price [20]替换为20

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

相关问题 使用jquery获取多个元素的属性值。 然后用它们 - Using jquery to get attribute value for multiple elements. Then use them 具有相同ID的多个元素获得名称 - Multiple elements with same id get name 如何从 URL 获取多个具有相同名称但具有不同且唯一索引的参数? - How to get multiple parameters with same name from a URL but with different and unique index? 如何通过name属性获取输入元素的集合 - How to get a collection of input elements by name attribute 如何从JavaScript中的URL获取具有相同名称的多个参数 - How to get multiple parameters with same name from a URL in JavaScript 如何通过正在更改的类名获取多个元素? - How to get multiple elements by class name that are changing? 如何访问具有相同类名的元素的数据属性? - How do I access data attribute of elements with the same class name? 从具有相同 class 名称的多个元素打印多个文本 - Print multiple texts from multiple elements with same class name 如何从同名元素中获取当前的Combo /文本框ID(可在新添加的元素上使用)? - How to get current Combo / textbox id (workable on new added elements) from same name elements? 如何使用jquery或javascript获取具有相同名称属性的多个文件的值? - How do I get the values of multiple fileds having same name attribute using jquery or javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM