简体   繁体   English

进行Java脚本/ jQuery测验:下拉菜单任务/离子未验证

[英]Making a Javascript/jQuery Quiz: Dropdown Menu Quest/ion Not Validating

https://jsfiddle.net/boriskay/thsmdqrt/3/ https://jsfiddle.net/boriskay/thsmdqrt/3/

In the JS code you can see that the text input questions are validated through an array of objects, but this method doesn't work for the dropdown question(the correct answer is "Botas"). 在JS代码中,您可以看到文本输入问题是通过对象数组验证的,但是此方法不适用于下拉问题(正确的答案是“ Botas”)。

How can I validate the correct answer? 如何验证正确答案?

Thank you in advance. 先感谢您。

HTML: HTML:

<div id="map">
  <img src="http://www.homedepot.com/catalog/productImages/1000/eb/ebd83d8b-6e6f-4526-8dd9-e063f0ef66bd_1000.jpg">
  <form>    
    <div id="q1C"><input id="q1" type="text" /></div>
    <div id="q2C"><input id="q2" type="text" /></div>
    <div id="q3C">
            <select id="q3">
                <option value="zapantandos">Zapatandos</option>
                <option value="botas">Botas</option>
                <option value="bripantas" id="truck">Bripantas</option>
            </select>
        </div>
        <input type="submit" id="submit" value="Click When Done" />
  </form>
</div>

JAVASCRIPT/JQUERY: JAVASCRIPT / JQUERY:

var answers = {
    "q1": ["Camisas", "camisas", "CAMISAS"],
    "q2": ["Zapatos", "zapatos", "ZAPATOS"],
    "q3": ["Botas", "botas", "BOTAS"]
};

function markAnswers() {
  $("input[type='text']").each(function() {
    console.log($.inArray(this.value, answers[this.id]));

    if($.inArray(this.value, answers[this.id]) === -1) {
      $(this).parent().append("<img class='result_pic' src='https://cdn4.iconfinder.com/data/icons/geomicons/32/672366-x-128.png' />");

    } else {
      $(this).parent().append("<img class='result_pic' src='http://image.flaticon.com/icons/png/128/74/74414.png' />");
      //$("form").append("Great job!");
    }
  })
}

$("form").on("submit", function(e){
    e.preventDefault();
    markAnswers();
    $("form").append("Great job!");
});

CSS: CSS:

#q1C, 
#q2C,
#q3C{
    position: absolute;
    display:flex;
    z-index:100;
  align-items:center;
}

#q1,#q2,#q3{
  height: 20px;
  float:left;
}

#q1C{   
    margin-top: -450px;
    margin-left: 350px; 
}

#q2C{   
    margin-top: -160px;
    margin-left: 350px;
}

#q3C{   
    margin-top: -610px;
    margin-left: 475px;
}

.result_pic {
    position: relative;
  z-index:100;
  width:20px;
  height:20px;
  margin-left:5px;
}

#submit {
    height: 35px;
    font-size: 17px;
    background-color: skyblue;
}

ol {
    list-style: none;
}

Your .each loop is selecting this: 您的.each循环正在选择此选项:

input[type='text']

...which will not cover the select element. ...不会覆盖select元素。 Try this instead: 尝试以下方法:

$("input[type='text'], select").each(function() {
  //...
});

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

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