我有一些输入字段,名称相同,但我只想像这样获取每个输入的索引值
<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">
所以,例如,我只想从 op_price 名称属性中获取 20、2、14,是他们的任何 JS 或 jquery 方法来执行此操作
最佳答案
这将返回数组中的索引:
var indexes = $('[name^="op_price"]').map(function(){
return this.name.match(/\d+/);
}).get();
console.log(indexes); // ["20", "2", "14"]
关于javascript - 如何从多个元素的相同名称中获取名称属性的索引。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18437993/