【发布时间】:2022-01-15 06:57:12
【问题描述】:
我有一个带有预定义数组键的 html 输入数组(进一步操作需要预定义键),如下所示:
<input type="hidden" name="tests[5]" value="b">
<input type="hidden" name="tests[8]" value="a">
我尝试使用这样的 jQuery 方法获取值:
var testsValues = $('input[name="tests[]"]').map(function () {
return this.value; // $(this).val()
}).get();
console.log(testsValues);
它总是给出一个空的结果,但是如果我删除预定义的键它不会给出一个空的结果
<input type="hidden" name="tests[]" value="b">
<input type="hidden" name="tests[]" value="a">
使用预定义键检索输入数组值的正确方法是什么?
【问题讨论】:
-
试试
input[name^=tests]。 w3schools.com/cssref/sel_attr_begin.asp -
@RatajS 谢谢我可以检索值,但我怎样才能检索预定义的数组键?
-
我在下面编辑了我的代码