gpt4 book ai didi

javascript - HTML - 如何显示下拉菜单中的选定值并将其定向到新的 HTML 页面

转载 作者:行者123 更新时间:2023-11-28 04:15:43 31 4
gpt4 key购买 nike

如何将下拉列表中的选定值从 HTML 页面显示/重定向到新页面?例如,如果我从下拉列表中选择任何值,当我单击“GO”按钮时,页面将定向到一个新页面,并显示所选值。在这方面需要帮助。

<!DOCTYPE html>
<html>

<body>
<div align="center">

<center>
<h4 style="color:darkblue">Choose Your Food/Beverage & Status : </h3>
</center>

<table>
<tr>
<td>

<font size=2>
<B>Choose a Food/Beverage : </B>
</font>

<select ID="foodbeverage">

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

<optgroup label="Food">
<option value = "chickenchop">Chicken Chop</option>
<option value = "pasta">Pasta</option>
<option value = "pizza">Pizza</option>
<option value = "chocolate">Chocolate Cake</option>
<option value = "redvelvet">Red Velvet Cake</option>
<option value = "icecream">Ice Cream Cake</option>
</optgroup>

<optgroup label="Beverages">
<option value = "milk">Milk</option>
<option value = "freshjuice">Fresh Juice</option>
<option value = "icecream">Ice Cream</option>
<option value = "coffee">Coffee</option>
<option value = "carbonated">Carbonated Can Drink</option>
<option value = "water">Water</option>
</optgroup>

</select>
<br/>

<font size=2>
<B>Choose a Food/Beverage : </B>
</font>

<select ID="foodbeverage">

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

<optgroup label="Food">
<option value = "chickenchop">Chicken Chop</option>
<option value = "pasta">Pasta</option>
<option value = "pizza">Pizza</option>
<option value = "chocolate">Chocolate Cake</option>
<option value = "redvelvet">Red Velvet Cake</option>
<option value = "icecream">Ice Cream Cake</option>
</optgroup>

<optgroup label="Beverages">
<option value = "milk">Milk</option>
<option value = "freshjuice">Fresh Juice</option>
<option value = "icecream">Ice Cream</option>
<option value = "coffee">Coffee</option>
<option value = "carbonated">Carbonated Can Drink</option>
<option value = "water">Water</option>
</optgroup>

</select>
<br/>

</td>

<td>
<font size=2>
<B>Dine In or Take Away : </B>
</font>
<select ID="status">

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

<optgroup label="STATUS">
<option value = "dinein">Dine In</option>
<option value = "takeaway">Take Away</option>
</optgroup>
</select>
<br/>

<font size=2>
<B>Dine In or Take Away : </B>
</font>
<select ID="status">

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

<optgroup label="STATUS">
<option value = "dinein">Dine In</option>
<option value = "takeaway">Take Away</option>
</optgroup>
</select>
<br/>

</td>
</tr>
</table>

<br/>
<form method="get" action="newPage.html">
<input type="submit" value=" GO " />
</form>
<br/>

</body>

</html>

在此页面中,我希望相应地显示/重定向值。

<!DOCTYPE html>
<html>

<body>
<div align="center">

<font size=4>
<B>Food/Beverage Selected : </B>
</font>

<br/>
<br/>

<font size=4>
<B>Dine In/Take Away : </B>
</font>

<br/>
<br/>

</body>

</html>

最佳答案

解释

据我所知,最简单的方法是使用表单和 PHP 代码来填充其他页面。为了将数据从一个页面传递到另一个页面,您使用了表单。但您需要将您的选择放入表单中。

然后,为了在第二页中显示数据,您需要一些 PHP 代码。因此,您需要将 newPage.html 重命名为 newPage.php

运行 PHP

当然,要运行 PHP,您需要一台服务器。如果您没有,我建议您安装其中之一:

  • WAMp对于窗口
  • MAMP对于Mac
  • XAMP适用于 Linux(也可在 Window 和 Mac 上使用。

代码

<!DOCTYPE html>
<html>

<body>
<div align="center">

<center>
<h4 style="color:darkblue">Choose Your Food/Beverage & Status : </h3>
</center>

<form method="get" action="newPage.php">
<div>
<label for='foodbeverage'>Choose a Food/Beverage : </label>

<select ID="foodbeverage" name='foodbeverage[]'>
<optgroup label="DEFAULT">
<option value="NONE">NONE</option>
</optgroup>
<optgroup label="Food">
<option value="chickenchop">Chicken Chop</option>
<option value="pasta">Pasta</option>
<option value="pizza">Pizza</option>
<option value="chocolate">Chocolate Cake</option>
<option value="redvelvet">Red Velvet Cake</option>
<option value="icecream">Ice Cream Cake</option>
</optgroup>
<optgroup label="Beverages">
<option value="milk">Milk</option>
<option value="freshjuice">Fresh Juice</option>
<option value="icecream">Ice Cream</option>
<option value="coffee">Coffee</option>
<option value="carbonated">Carbonated Can Drink</option>
<option value="water">Water</option>
</optgroup>
</select>

<label for='status'>
Dine In or Take Away :
</label>
<select ID="status" name='status[]'>

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

<optgroup label="STATUS">
<option value="dinein">Dine In</option>
<option value="takeaway">Take Away</option>
</optgroup>
</select>

</div>
<br/>

<div>
<label for='foodbeverage2'>Choose a Food/Beverage : </label>

<select ID="foodbeverage2" name='foodbeverage[]'>
<optgroup label="DEFAULT">
<option value="NONE">NONE</option>
</optgroup>
<optgroup label="Food">
<option value="chickenchop">Chicken Chop</option>
<option value="pasta">Pasta</option>
<option value="pizza">Pizza</option>
<option value="chocolate">Chocolate Cake</option>
<option value="redvelvet">Red Velvet Cake</option>
<option value="icecream">Ice Cream Cake</option>
</optgroup>
<optgroup label="Beverages">
<option value="milk">Milk</option>
<option value="freshjuice">Fresh Juice</option>
<option value="icecream">Ice Cream</option>
<option value="coffee">Coffee</option>
<option value="carbonated">Carbonated Can Drink</option>
<option value="water">Water</option>
</optgroup>
</select>

<label for='status2'>
Dine In or Take Away :
</label>
<select ID="status2" name="status[]">

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

<optgroup label="STATUS">
<option value="dinein">Dine In</option>
<option value="takeaway">Take Away</option>
</optgroup>
</select>

</div>


<br/>
<input type="submit" value=" GO "/>
</form>

</div>

</body>

</html>

这是您的 newPage.php 文件。

<!DOCTYPE html>
<html>

<body>
<div align="center">

<?
for ($i = 0; $i < sizeof($_GET['foodbeverage']); $i++) {
echo '<span style="font-size: medium; "><B>Food/Beverage Selected : '
. $_GET['foodbeverage'][$i]
. '</B></span>'
. '<br/><br/>'
. '<span style="font-size: medium; "><B>Dine In/Take Away : '
. $_GET['status'][$i]
. '></B></span><br/><br/>';
}
?>

</div>

</body>

</html>

希望有帮助。

编辑:

当然,服务器端并不强制你使用PHP。如果您使用其他语言也没关系。

关于javascript - HTML - 如何显示下拉菜单中的选定值并将其定向到新的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873466/

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