gpt4 book ai didi

javascript - HTML - 多个下拉列表的数组

转载 作者:行者123 更新时间:2023-11-30 21:13:26 25 4
gpt4 key购买 nike

在这里,我创建了两行下拉列表,每行分别包含“食品/饮料”和“堂食/外卖”。我如何创建一个允许显示多个值的数组。

此代码 (mainTest.html) 在有多个值时显示两行下拉列表。

(function() {
/**
* Handles the click of the submit button.
*/
function onSubmitClicked(event) {
// Get the input element from the DOM.
var beverage = document.getElementById('foodbeverage1');
var status = document.getElementById('status1');
// Get the value from the element.
var beverageValue = beverage.value;
var status = status.value;
// Construct the URL.
var url = 'newPageTest.html?foodbeverage1=' + encodeURIComponent(beverageValue) + '&status1=' + encodeURIComponent(status);

// Instead of going to the URL, log it to the console.
location.href = url;
}
// Get the button from the DOM.
var submitButton = document.getElementById('btngo');
// Add an event listener for the click event.
submitButton.addEventListener('click', onSubmitClicked);
})();
<!DOCTYPE html>
<html>

<body>

<h4 style="color:darkblue">Choose Your Food/Beverage & Quantity : </h4>

<table>
<tr>
<td>

<B>Choose a Food/Beverage : </B>

<select ID="foodbeverage1">

<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>

<optgroup label="Food">
<option value = "Chicken Chop">Chicken Chop</option>
<option value = "Pasta">Pasta</option>
<option value = "Pizza">Pizza</option>
<option value = "Chocolate Cake">Chocolate Cake</option>
<option value = "Red Velvet Cake">Red Velvet Cake</option>
<option value = "Ice Cream Cake">Ice Cream Cake</option>
</optgroup>

<optgroup label="Beverages">
<option value = "Milk">Milk</option>
<option value = "Fresh Juice">Fresh Juice</option>
<option value = "Ice Cream">Ice Cream</option>
<option value = "Coffee">Coffee</option>
<option value = "Carbonated Can Drink">Carbonated Can Drink</option>
<option value = "Water">Water</option>
</optgroup>

</select>
<br/>

<B>Choose a Food/Beverage : </B>

<select ID="foodbeverage2">

<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>

<optgroup label="Food">
<option value = "Chicken Chop">Chicken Chop</option>
<option value = "Pasta">Pasta</option>
<option value = "Pizza">Pizza</option>
<option value = "Chocolate Cake">Chocolate Cake</option>
<option value = "Red Velvet Cake">Red Velvet Cake</option>
<option value = "Ice Cream Cake">Ice Cream Cake</option>
</optgroup>

<optgroup label="Beverages">
<option value = "Milk">Milk</option>
<option value = "Fresh Juice">Fresh Juice</option>
<option value = "Ice Cream">Ice Cream</option>
<option value = "Coffee">Coffee</option>
<option value = "Carbonated Can Drink">Carbonated Can Drink</option>
<option value = "Water">Water</option>
</optgroup>

</select>
<br/>

</td>

<td>
<B>Dine In or Take Away : </B>
<select ID="status1">
<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>
<optgroup label="Status">
<option value = "Dine In">Dine In</option>
<option value = "Take Away">Take Away</option>
</optgroup>
</select>
<br/>

<B>Dine In or Take Away : </B>
<select ID="status2">
<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>
<optgroup label="Status">
<option value = "Dine In">Dine In</option>
<option value = "Take Away">Take Away</option>
</optgroup>
</select>
<br/>
</td>
</tr>
</table>
<br/>
<br/>

<input type="submit" id="btngo" value="Go" />

<br/>


</body>

</html>

此代码 (newPageTest.html) 显示从 mainTest.html 中选择的值。

window.onload = passParameters;

//Function to update "showdata" div with URL Querystring parameter values
function passParameters() {
var foodbeverage = getParameterByName("foodbeverage1");
var status = getParameterByName("status1");
if (foodbeverage != null && status != null) {
var data = "<b>Food Beverages:</b> " + foodbeverage + " <b>Dine/Takeaway:</b> " + status;
document.getElementById("showdata").innerHTML = data;
}
}
//Get URL parameter value
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
<!DOCTYPE html>
<html>

<body>
<div id="showdata"></div>
</body>

</html>

最佳答案

通常数组显示为具有相同名称的多个参数。一些 (PHP) 人用 [] 命名他们的数组参数或 [<num>]后缀也是如此。

var url = 'newPageTest.html?foodbeverage=' + 
encodeURIComponent(document.getElementById('foodbeverage1').value) +
'&foodbeverage=' +
encodeURIComponent(document.getElementById('foodbeverage2').value) +
'&status=' +
encodeURIComponent(document.getElementById('status1').value) +
'&status=' +
encodeURIComponent(document.getElementById('status2').value);

如果您对表单使用多项选择,它会自动为您完成。如果您自己构建查询字符串,则按类(html 中的 <select class="foodbeverage">)获取元素并遍历元素以生成 url 会更容易。

(function() {

var url = 'newPageTest.html?';

var foodbevs = document.getElementsByClassName('foodbeverage');
for (var i = 0; i < foodbevs.length; i++) {
if (i > 0) url += '&';
url += 'foodbeverage=' + encodeURIComponent(foodbevs[i].value)
}

var statuses = document.getElementsByClassName('status');
for (i = 0; i < statuses.length; i++) {
url += '&status=' + encodeURIComponent(statuses[i].value)
}

alert(url); // Show the url for testing

location.href = url;
})();
    <body>

<h4 style="color:darkblue">Choose Your Food/Beverage & Quantity : </h4>

<table>
<tr>
<td>

<B>Choose a Food/Beverage : </B>

<select class="foodbeverage">

<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>

<optgroup label="Food">
<option value = "Chicken Chop">Chicken Chop</option>
<option value = "Pasta">Pasta</option>
<option value = "Pizza">Pizza</option>
<option value = "Chocolate Cake">Chocolate Cake</option>
<option value = "Red Velvet Cake">Red Velvet Cake</option>
<option value = "Ice Cream Cake">Ice Cream Cake</option>
</optgroup>

<optgroup label="Beverages">
<option value = "Milk">Milk</option>
<option value = "Fresh Juice">Fresh Juice</option>
<option value = "Ice Cream">Ice Cream</option>
<option value = "Coffee">Coffee</option>
<option value = "Carbonated Can Drink">Carbonated Can Drink</option>
<option value = "Water">Water</option>
</optgroup>

</select>
<br/>

<B>Choose a Food/Beverage : </B>

<select class="foodbeverage">

<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>

<optgroup label="Food">
<option value = "Chicken Chop">Chicken Chop</option>
<option value = "Pasta">Pasta</option>
<option value = "Pizza">Pizza</option>
<option value = "Chocolate Cake">Chocolate Cake</option>
<option value = "Red Velvet Cake">Red Velvet Cake</option>
<option value = "Ice Cream Cake">Ice Cream Cake</option>
</optgroup>

<optgroup label="Beverages">
<option value = "Milk">Milk</option>
<option value = "Fresh Juice">Fresh Juice</option>
<option value = "Ice Cream">Ice Cream</option>
<option value = "Coffee">Coffee</option>
<option value = "Carbonated Can Drink">Carbonated Can Drink</option>
<option value = "Water">Water</option>
</optgroup>

</select>
<br/>

</td>

<td>
<B>Dine In or Take Away : </B>
<select class="status">
<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>
<optgroup label="Status">
<option value = "Dine In">Dine In</option>
<option value = "Take Away">Take Away</option>
</optgroup>
</select>
<br/>

<B>Dine In or Take Away : </B>
<select class="status">
<optgroup label="DEFAULT">
<option value = "NONE">NONE</option>
</optgroup>
<optgroup label="Status">
<option value = "Dine In">Dine In</option>
<option value = "Take Away">Take Away</option>
</optgroup>
</select>
<br/>
</td>
</tr>
</table>
<br/>
<br/>

<input type="submit" id="btngo" value="Go" />

<br/>


</body>

但是,您在结果页面上对参数的解析似乎没有正确处理数组。请在此处查看答案以获得更好的查询字符串解析器 Parse query string in JavaScript

function parseQuery(str)
{
if(typeof str != "string" || str.length == 0) return {};
var s = str.split("&");
var s_length = s.length;
var bit, query = {}, first, second;
for(var i = 0; i < s_length; i++)
{
bit = s[i].split("=");
first = decodeURIComponent(bit[0]);
if(first.length == 0) continue;
second = decodeURIComponent(bit[1]);
if(typeof query[first] == "undefined") query[first] = second;
else if(query[first] instanceof Array) query[first].push(second);
else query[first] = [query[first], second];
}
return query;
}

//Function to update "showdata" div with URL Querystring parameter values
function passParameters() {
var queryStr = window.location.search

/////////////////////////////////////////////
// Remove the following line and add ; at end of line above if not testing.
|| 'foodbeverage=Pizza&foodbeverage=Pasta&status=Dine+In&status=Take+Away';
////////////////////////////////////////////

var query = parseQuery(queryStr);
var data = "<b>Food Beverages:</b> " + query.foodbeverage + " <b>Dine/Takeaway:</b> " + query.status;
document.getElementById("showdata").innerHTML = data;
}

window.onload = passParameters;
<html>

<body>
<div id="showdata"></div>
</body>

</html>

上面query.foodbeverage和query.status可以是数组。您可以遍历它们以逐行打印它们。

关于javascript - HTML - 多个下拉列表的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45911135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com