我有这样的foreach php:
@foreach($posts as $post)
<h2>{{$post->title}}</h2>
<img src="{{$post->image}}" width="150" height="150">
<p>{{$post->country}}</p>
<p>{{$post->zone}}</p>
<p>{{$post->user->name}}</p>
<input type="hidden" class="postId" value="{{$post->id}}" name="postId">
<p class="expiredate">{{$post->expire_date}}</p>
<p class="current">{{$current}}</p>
@endforeach
我想用输入hiddens的值来做一个数组javascript,如下所示:
var inputsArray= [value first input, value second input, value....]
我是这样尝试的:
var d = document;
var inputsArray = d.querySelectorAll('.postId');
但它不起作用,我的控制台给了我:
console.log(inputArray.value) = undefined
答案 0 :(得分:2)
您需要将变量转换为true数组,因为querySelectorAll
返回节点列表,而不是数组。然后迭代该数组,因为数组本身没有value
属性:
var d = document;
var inputsArray = Array.from(d.querySelectorAll('.postId'));
inputsArray.forEach(function (input) {
console.log(input.value);
});