Javascript-使用输入值创建数组 [英] Javascript - Create array with inputs values
本文介绍了Javascript-使用输入值创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的foreach php:
I have a foreach php like this:
@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
我想做一个数组javascript,其输入值是隐藏的,像这样:
I would like do an array javascript with values of inputs hiddens, like this:
var inputsArray= [value first input, value second input, value....]
我像这样尝试:
var d = document;
var inputsArray = d.querySelectorAll('.postId');
但是它不起作用,我的控制台给我:
but it doen't work, my console give me:
console.log(inputArray.value) = undefined
推荐答案
您需要将变量转换为真实数组,因为 querySelectorAll
返回的是节点列表,而不是数组。然后遍历该数组,因为数组本身没有 value
属性:
You need to convert your variable to a true array, since querySelectorAll
returns a node list, not an array. Then iterate over that array, as the array itself has no value
property:
var d = document;
var inputsArray = Array.from(d.querySelectorAll('.postId'));
inputsArray.forEach(function (input) {
console.log(input.value);
});
这篇关于Javascript-使用输入值创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文