如何将表单选择转到另一页?

时间:2016-06-20 17:10:05

标签: javascript html forms hyperlink

我正在为课程项目创建调查,其中一个调查问题需要一个下拉列表,用户将在其中选择一个字母。这个想法是当用户选择一个字母时,代码会将它们链接到另一个网站。例如," A"将他们带到X网站," B将他们带到X网站," N"将他们带到Y等网站......

我对编程世界很陌生并且了解HTML和JavaScript的基础知识,因此我的调查是HTML和JavaScript。我对任何建议持开放态度。我不确定是否可以将链接添加到HTML中,或者我是否需要像JavaScript代码中那样创建一个onclick函数。任何方向都会非常感激!



var StoryMap = function(){
  var elements = document.getElementsByTagName("option");
  if (elements === "A"){
    alert("Thank you! Please go to this Story Map (link)");
  }else{
    alert("Please select the first letter of your last name");
  }
};

<!DOCTYPE html>
<html>

<head>
<title>Esri Story Map Study</title>
<script src="javascript.js"></script>
<link href="style.css" type="text/css" rel="stylesheet"/>
</head>

<body>
<h2>The Windom Park Historic Residential District Pre-Survey</h2>

<h3>Please select the <span>first letter</span> of your <span>last name</span>: </h3>

<form>
  <p>
    <select name="firstLetter">
      <option value="Blank"></option>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="F">F</option>
      <option value="G">G</option>
      <option value="H">H</option>
      <option value="I">I</option>
      <option value="J">J</option>
      <option value="K">K</option>
      <option value="L">L</option>
      <option value="M">M</option>
      <option value="N">N</option>
      <option value="O">O</option>
      <option value="P">P</option>
      <option value="Q">Q</option>
      <option value="R">R</option>
      <option value="S">S</option>
      <option value="T">T</option>
      <option value="U">U</option>
      <option value="V">V</option>
      <option value="W">W</option>
      <option value="X">X</option>
      <option value="Y">Y</option>
      <option value="Z">Z</option>
  </p>
<input type="submit" name="next" value="Next"/>
</form>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

document.getElementsByValue(&#34;选项&#34);

答案 1 :(得分:0)

如果我没有弄错,你可以让你的选项值等于你想要的网站。例如:

<select name="firstletter">
    <option value="www.google.com">A</option>
    <option value="www.amazon.com">B</option>
    <option value="www.yahoo.com"> C</option>
</select>

答案 2 :(得分:0)

这有助于你:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <script>
            function StoryMap(){
              var ele = document.forms["frm"]["firstLetter"].value;
              if (ele == "A"){
                var link = "https://www.google.com";
                alert("Thank you! Please go to this Story Map " + link);
               }
               else{
                alert("Please select the first letter of your last name");
              }
            }
        </script>
        
        <h3>Please select the <span>first letter</span> of your <span>last name</span>: </h3>

<form name="frm" method="post" onsubmit="StoryMap()">
  <p>
    <select name="firstLetter">
      <option value="Blank"></option>
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="F">F</option>
      <option value="G">G</option>
      <option value="H">H</option>
      <option value="I">I</option>
      <option value="J">J</option>
      <option value="K">K</option>
      <option value="L">L</option>
      <option value="M">M</option>
      <option value="N">N</option>
      <option value="O">O</option>
      <option value="P">P</option>
      <option value="Q">Q</option>
      <option value="R">R</option>
      <option value="S">S</option>
      <option value="T">T</option>
      <option value="U">U</option>
      <option value="V">V</option>
      <option value="W">W</option>
      <option value="X">X</option>
      <option value="Y">Y</option>
      <option value="Z">Z</option>
      </select>
<input type="submit" name="next" value="Next"/>
</form>
    </body>
</html>

答案 3 :(得分:0)

如果您没有向您的&#34;新页面发送数据&#34;然后你不需要form,可能还需要按钮。代码非常简单。

<select onchange="redirToDestination(this)">
  <option value="Blank"></option>
  <option value="A">A</option>
  <option value="B">B</option>
  <option value="C">C</option>
  <option value="D">D</option>
  <option value="E">E</option>
  <option value="F">F</option>
</select>

...
function redirToDestination(elem){
   switch(elem.value){
     case 'A':
       window.location = 'newpage1.html';
       break;
     case 'B':
     case 'C':
       window.location = 'newpage2.html';
       break;
     ...
     default:
       alert('Select letter');
       break;
   }//switch
}//function