在将所有项目单独输出到控制台后,如何创建项目数组



正如您目前所看到的,代码的结果是将选项列表的每个值输出到控制台。然而,我希望它们被分组到一个数组变量中,而不是由代码一个接一个地输出。感谢您的帮助!

(function($){
$('.request-poc').attr('href', function() {
var product = $(this).attr("data-product");
return this.href + '?product=' + product;
});
if ($('body.request-poc').length > 0)
{
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var product = GetURLParameter('product');
console.log(product)
$('#product').val(product);
}
Array.from(document.querySelector("#product").options).forEach(function(option_element) {
let option_value = option_element.value;
console.log(option_value);
}); 
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body class='request-poc'>
<h1>Dummy</h1>
<form action="#">
<select name="Select product" id="product">
<option value="SHOES">Shoes</option>
<option value="SHIRT">Shirt</option>
<option value="JUMPER">Jumper</option>
<option value="SOCKS">Socks</option>
<option value="JEANS">Jeans</option>
<option value="OTHER">Other</option>
</select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="script.js"></script>
</body>

您可以声明一个空数组并推送其中的所有选项。

(function ($) {
$(".request-poc").attr("href", function () {
var product = $(this).attr("data-product");
return this.href + "?product=" + product;
});
if ($("body.request-poc").length > 0) {
function GetURLParameter(sParam) {
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split("&");
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split("=");
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
}
var product = GetURLParameter("product");
console.log(product);
$("#product").val(product);
}
const arr = [];
Array.from(document.querySelector("#product").options).forEach(function (
option_element
) {
arr.push(option_element.value);
});
console.log(arr);
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body class='request-poc'>
<h1>Dummy</h1>
<form action="#">
<select name="Select product" id="product">
<option value="SHOES">Shoes</option>
<option value="SHIRT">Shirt</option>
<option value="JUMPER">Jumper</option>
<option value="SOCKS">Socks</option>
<option value="JEANS">Jeans</option>
<option value="OTHER">Other</option>
</select>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="script.js"></script>
</body>

你能在foreach循环外设置一个空数组,然后在循环中推到它吗

例如

var test =[]   
Array.from(document.querySelector("#product").options).forEach(function(option_element) {
let option_value = option_element.value;
console.log(option_value);
test.push(option_value)
}); 

最新更新