若否,;Switch语句不能处理HTML表单中的值(与JavaScript交互)

若否,;Switch语句不能处理HTML表单中的值(与JavaScript交互),javascript,html,if-statement,widget,radio-button,Javascript,Html,If Statement,Widget,Radio Button,我正在创建一个小部件,其中用户从一些单选按钮中选择一个选项,并通过JavaScriptCodeing创建对用户的响应(响应因用户选择的选项而异)。我可以让小部件显示用户在一句话中选择的选项(“您选择了“某物”,这里是您的建议:”) 然而,在这一行之后,我想创建一个实际给出用户推荐的语句(小部件的概念是:用户从选项中选择大学学位,程序将返回与学位相关的推荐科目) 每次我尝试If/else语句或“switch”语句时,在测试小部件时都没有发生任何事情(甚至没有显示“youhaveselect…”部分

我正在创建一个小部件,其中用户从一些单选按钮中选择一个选项,并通过JavaScriptCodeing创建对用户的响应(响应因用户选择的选项而异)。我可以让小部件显示用户在一句话中选择的选项(“您选择了“某物”,这里是您的建议:”)

然而,在这一行之后,我想创建一个实际给出用户推荐的语句(小部件的概念是:用户从选项中选择大学学位,程序将返回与学位相关的推荐科目)

每次我尝试If/else语句或“switch”语句时,在测试小部件时都没有发生任何事情(甚至没有显示“youhaveselect…”部分)。我已经包括了我的最后一次尝试。如果switch语句被删除,则响应的第一部分起作用;如果包含该语句,则按“提交”时不会发生任何事情

代码:


变量degreeChoice=(form.degree.value)
函数处理度(表格){
变量degreeChoice=(form.degree.value);
document.write(“您选择了“+”+degreeChoice+”,这是您的建议:“+”)
};
开关(除冰){
案例“农业”:
文件。书写(“它起作用”);
[中断;]
案例“艺术”:
文件。书写(“仍然有效”);
[中断;]
违约:
文件。写入(“发生错误”);
[中断;]
}
选择学位:
农业

艺术类
生物医学
商业
环境
美术
音乐
口腔健康
科学类

  • 加载后不能使用document.write-它将擦除页面
  • 您不能使用submit并期望停留在页面上。我将其移动到表单的onsubmit,并返回false表示不提交
  • 你有[休息]而不是仅仅休息
  • 有一个
    var degreeChoice=(form.degree.value)函数外部
  • 该函数在开关前结束,使用
    }
  • 试试这个:

    <!DOCTYPE html>
    <html>
    
    <script language="JavaScript">
      function processDegrees (form) {
        var degreeChoice = (form.degree.value);
        text="You chose " + degreeChoice + ", here are your recommendations: ";
    
        switch(degreeChoice) {
          case "agriculture":
            text += "It works";
            break;
          case "arts":
            text +="still works";
            break;
          default:
            text+="An error has occurred";
            break;
        }
        document.getElementById("result").innerHTML=text;
        return false; // cancel submission
      }
    </script>       
    
        <body>
    <form name="chooseDegrees" action=" " method="get" onsubmit="return processDegrees(this)">
        <h1>Choose Degrees:</h1>
      <input type="radio" name="degree" value="agriculture"> Agriculture
    <br>
      <input type="radio" name="degree" value="arts"> Arts
    <br> 
      <input type="radio" name="degree" value="biomedicine"> Biomedicine
    <br>
      <input type="radio" name="degree" value="commerce"> Commerce
    <br>
      <input type="radio" name="degree" value="environments"> Environments
    <br>
      <input type="radio" name="degree" value="fine arts"> Fine Arts
    <br>
      <input type="radio" name="degree" value="music"> Music
    <br>
      <input type="radio" name="degree" value="oral health"> Oral Health
    <br>
      <input type="radio" name="degree" value="science"> Science
    <br><br> 
    <input type="submit "value="Submit!">
    </form> 
    <div id="result"></div>    
    </body>
    </html> 
    
    
    选择学位:
    农业
    
    艺术类
    生物医学
    商业
    环境
    美术
    音乐
    口腔健康
    科学类


    [break;]导致语法错误-检查浏览器控制台,因为任何错误都有停止所有JavaScript执行的趋势。(按F12键,选择控制台选项卡以查看任何错误)。将[中断;]更改为中断;(去掉方括号)。谢谢你的回复,我看的教程一定是错的。欢迎你。我希望你不会太明显地得到了一些帮助。我还删除了一个不必要的
    “”+
    ,并在字符串中添加了空格
    <!DOCTYPE html>
    <html>
    
    <script language="JavaScript">
      function processDegrees (form) {
        var degreeChoice = (form.degree.value);
        text="You chose " + degreeChoice + ", here are your recommendations: ";
    
        switch(degreeChoice) {
          case "agriculture":
            text += "It works";
            break;
          case "arts":
            text +="still works";
            break;
          default:
            text+="An error has occurred";
            break;
        }
        document.getElementById("result").innerHTML=text;
        return false; // cancel submission
      }
    </script>       
    
        <body>
    <form name="chooseDegrees" action=" " method="get" onsubmit="return processDegrees(this)">
        <h1>Choose Degrees:</h1>
      <input type="radio" name="degree" value="agriculture"> Agriculture
    <br>
      <input type="radio" name="degree" value="arts"> Arts
    <br> 
      <input type="radio" name="degree" value="biomedicine"> Biomedicine
    <br>
      <input type="radio" name="degree" value="commerce"> Commerce
    <br>
      <input type="radio" name="degree" value="environments"> Environments
    <br>
      <input type="radio" name="degree" value="fine arts"> Fine Arts
    <br>
      <input type="radio" name="degree" value="music"> Music
    <br>
      <input type="radio" name="degree" value="oral health"> Oral Health
    <br>
      <input type="radio" name="degree" value="science"> Science
    <br><br> 
    <input type="submit "value="Submit!">
    </form> 
    <div id="result"></div>    
    </body>
    </html>