[英]how to get data from inputs as array using Jquery?
hello guys i have many input fields and i want get it as array data like this大家好,我有很多输入字段,我想把它作为这样的数组数据
array (
'gender' => "male",
"height" => "180",
'weight' => '90',
'value' = '#55'
),
array (
'gender' => "male",
"height" => "177",
'weight' => '68',
'value' = '#66'
),
array (
'gender' => "female",
"height" => "150",
'weight' => '55',
'value' = '#77'
),
and this is the input fields that I want to export it into array data as I showed you这是我想要将其导出到数组数据中的输入字段,正如我向您展示的那样
<input type="text" gender='male' height='180' weight='90' value='#55' class='get-my-data'>
<input type="text" gender='male' height='177' weight='68' value='#66' class='get-my-data'>
<input type="text" gender='female' height='150' weight='55' value='#77' class='get-my-data'>
If you want to actually use JQuery and not vanilla JS that is preferred, something like that should work:如果你想实际使用 JQuery 而不是首选的 vanilla JS,那么类似的东西应该可以工作:
const data = $('.get-my-data').map(function() {
return {
gender: $(this).attr('gender'),
height: $(this).attr('height'),
weight: $(this).attr('weight'),
value: $(this).attr('value')
};
}).get();
and you would get an array of objects: [{...}, {...}, ...]你会得到一个对象数组:[{...}, {...}, ...]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.