[英]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个输入字段的值!
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.