【发布时间】:2020-10-17 19:29:41
【问题描述】:
我的 HTML 表单有问题。我想让网站访问者选择一个地方,然后选择西西里岛的两个地方之一会将他重定向到一个特定的网页,根据选择的地方一个与另一个不同(并且暂时 2 个重定向文件在与主文件相同的文件夹,分别是“Book=Catania”和“Book=Mondello”)。
我使用了<input type = "text"> 和一些“列表”建议。对于答案,我使用了带有 if 条件的 JavaScript 脚本。
<html>
<head>
...
</head>
<body>
<SCRIPT LANGUAGE = "JavaScript">
function checkCheckBox (f) {
if (f.mondello.checked == true)
{
window.location.href = 'book=mondello.html';
return false;
}
if (f.catania.checked == true)
{
window.location.href = 'book=catania.html';
return false;
}}
</script>
<form action = "" method = "POST" onsubmit = "return checkCheckBox (this)">
<fieldset>
<br/>
<legend> Indicates the place </legend>
<input type = "text" size = "40" list = "places">
<datalist id = "places">
<option value = "Mondello, PA, Sicily" name = "mondello">
<option value = "Catania, CT, Sicily" name = "catania">
</datalist>
</body>
</html>
【问题讨论】:
-
book=catania.html。那不是 URL...尝试做./book=catania.html。尽管在 URL 中包含 = 对我来说并不合适,除非在查询参数中。 -
将
onchange事件监听器添加到数据列表或选择列表中,设置值选项属性以匹配您的重定向文件,然后您只需获取所选选项的值即可转发。
标签: javascript html list forms option