Javascript 如何在jquery中从输入字段中获取多个值?
我想使用jquery获取foreach循环中的输入字段值。下面是html结构Javascript 如何在jquery中从输入字段中获取多个值?,javascript,php,jquery,laravel,Javascript,Php,Jquery,Laravel,我想使用jquery获取foreach循环中的输入字段值。下面是html结构 @foreach ($products as $product) <div class = "form-row"> <input type="text" name="quantity" class="form-control quantity"value="{{$p
@foreach ($products as $product)
<div class = "form-row">
<input type="text" name="quantity" class="form-control quantity"value="{{$product->quantity }}">
<input type="text" name="price" class="form-control price"value="{{$product->price}}">
</div>
@endforeach
但这样,我只得到第一行的值。如何使用jquery获取所有行的值?您必须遍历所有元素才能从中获取值 您可以尝试jQuery的和来获取数组中的所有值 演示:
var quantityArr=$('.quantity').map(函数(){
return+this.value;
}).get();
控制台日志(quantityArr);
//如果要将值逗号分隔,请加入数组
var quantityCommaSeperatedString=quantityArr.join(',');
日志(QuantityCommaseOperatedString);
var priceArr=$('.price').map(函数(){
return+this.value;
}).get();
控制台日志(priceArr);
//如果要将值逗号分隔,请加入数组
var priceCommaseOperatedString=priceArr.join(',');
console.log(priceCommaseOperatedString)代码>
如果我只想得到具体的行数量,假设我点击第二行,我将得到第二行数量值,如果我点击第五行,我将只得到第五行数量值。怎么做?@ihprince,我在HTML中看不到任何行。请更新该场景的HTML…更新了HTML。如果$products有10个产品,则div将显示在10行中。@ihprince,添加了另一个答案,请检查:)
var quantity = $('.quantity').val();
var price= $('.price').val();