简体   繁体   English

从提交的表单中获取数据

[英]Getting data from a submitted form

I have a very simple HTML quiz. 我有一个非常简单的HTML测验。 It asks the user few questions about what kind of car they're looking for and suggests one. 它向用户询问有关他们正在寻找哪种汽车的几个问题,并提出了建议。 But I can't get the javascript to work. 但是我无法使JavaScript正常工作。 It's a very simple script so I don't know what I'm mising. 这是一个非常简单的脚本,所以我不知道自己在想什么。 Any help would be appreciated. 任何帮助,将不胜感激。

<form id="form" action="">
<p>What size do you need?</p>
<input type="radio" name="size" value="compact">Compact<br>
<input type="radio" name="size" value="mid">Midsize<br>
<input type="radio" name="size" value="small">Small SUV<br>
<input type="radio" name="size" value="med">Medium SUV<br>
<input type="radio" name="size" value="big">Big SUV

<p>What kind of driving will you be doing?</p>
<input type="radio" name="drive" value="city">City/suburb driving<br>
<input type="radio" name="drive" value="mountain">Mountain driving

<p>Which would you rather have?</p>
<input type="radio" name="mpg" value="mpg">Good gas milage<br>
<input type="radio" name="mpg" value="performance">Performance

<p>How much do luxury features matter to you?</p>
<input type="radio" name="money" value="money">I'd rather save money and have less luxury features<br>
<input type="radio" name="money" value="luxury">I'd rather spend a little more and have luxury features.

<p><input type="submit" value="submit"></p>
</form>

Javascript: Javascript:

document.getElementById("form").onsubmit=function() {
    size = document.querySelector('input[name = "size"]:checked').value;
    drive = document.querySelector('input[name = "drive"]:checked').value;
    mpg = document.querySelector('input[name = "mpg"]:checked').value;
    money = document.querySelector('input[name = "money"]:checked').value;         

    if (size == "compact" && drive == "city" && mpg == "mpg" && money == "money") 
    {
        result = "Prius";
    }   else {
        result = "something else";
    }
    document.getElementById("suggestion").innerHTML = result; 
return false; // required to not refresh the page
}; 

Your code have several issues. 您的代码有几个问题。

First, you need to declare your variable using var . 首先,您需要使用var声明变量。 You need to set a default value for each radio button. 您需要为每个单选按钮设置默认值。 Also from your example document.getElementById("suggestion") is never into the DOM. 同样从您的示例document.getElementById("suggestion")永远不会进入DOM。

Here a working demo 这是一个工作演示

One issue, you will notice that 一个问题,您会注意到

document.querySelector('input[name = "size"]:checked').value;

will return an error when you press your submit button. 当您按下提交按钮时,将返回错误。 This error maybe caused by a user not selecting an option. 此错误可能是由用户未选择选项引起的。 If the user does not select an option then 如果用户未选择选项,则

document.querySelector('input[name = "size"]:checked') is null. document.querySelector('input[name = "size"]:checked')为空。 therefore null.value is invalid and your console shows this. 因此null.value无效,您的控制台将显示此内容。

You can test each object if it is null by using 您可以使用以下方法测试每个对象是否为空:

 document.querySelector('input[name ="size"]:checked') ?

Here is a snippet 这是一个片段

 document.getElementById("form").onsubmit = function() { size = document.querySelector('input[name ="size"]:checked') ? document.querySelector('input[name ="size"]:checked').value : ""; drive = document.querySelector('input[name ="drive"]:checked') ? document.querySelector('input[name ="drive"]:checked').value : ""; mpg = document.querySelector('input[name ="mpg"]:checked') ? document.querySelector('input[name ="mpg"]:checked').value : ""; money = document.querySelector('input[name ="money"]:checked') ? document.querySelector('input[name ="money"]:checked').value : ""; if (size == "compact" && drive == "city" && mpg == "mpg" && money == "money") { result = "Prius"; } else { result = "something else"; } document.getElementById("suggestion").innerHTML = result; return false; // required to not refresh the page }; 
 <form id="form" action=""> <p>What size do you need?</p> <input type="radio" name="size" value="compact">Compact<br> <input type="radio" name="size" value="mid">Midsize<br> <input type="radio" name="size" value="small">Small SUV<br> <input type="radio" name="size" value="med">Medium SUV<br> <input type="radio" name="size" value="big">Big SUV <p>What kind of driving will you be doing?</p> <input type="radio" name="drive" value="city">City/suburb driving<br> <input type="radio" name="drive" value="mountain">Mountain driving <p>Which would you rather have?</p> <input type="radio" name="mpg" value="mpg">Good gas milage<br> <input type="radio" name="mpg" value="performance">Performance <p>How much do luxury features matter to you?</p> <input type="radio" name="money" value="money">I'd rather save money and have less luxury features<br> <input type="radio" name="money" value="luxury">I'd rather spend a little more and have luxury features. <p><input type="submit" value="submit"></p> <p id="suggestion">What kind of driving will you be doing?</p> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM