简体   繁体   English

Mozilla Firefox中的未定义错误

[英]Undefined error in Mozilla Firefox

Why I'm getting undefined error in Firefox and IE. 为什么我在Firefox和IE中收到未定义的错误。 This code works well in Google Chrome. 此代码在Google Chrome浏览器中效果很好。 Here is the full code http://liveweave.com/fUhpiI 这是完整的代码http://liveweave.com/fUhpiI

this is my html 这是我的HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="css/hpstyles.css" rel="stylesheet">
<script src="js/hpjs.js"></script>
</head>
<body>
<form id="hp">
    <div>
    <h2>1. Which one do you prefer?</h2>
    <div>   
        <input type="radio" name="q1" id="radio1" class="radio" value="9"/>
        <label for="radio1">Tea</label>
    </div>
    <div>   
        <input type="radio" name="q1" id="radio2" class="radio" value="4"/>
        <label for="radio2">Coffee</label>
    </div>
    <div>   
        <input type="radio" name="q1" id="radio3" class="radio" value="1"/>
        <label for="radio3">Milk</label>
    </div>
    </div>

    <div>
    </br>
    <div><div>
    <button type="button" onclick="hp(this.form)">Check</button>
    <input class="reset" type="reset" value="Reset">
    </div></div></div>
</form>
    <div id="result"></div>
    <div id="total"></div>
</body>
</html>

this is javascript 这是javascript

function hp(form)
{
var count1=0, count2=0, count3=0, count4=0, count5=0, count6=0, count7=0, count8=0, count9=0, count10=0,a ;
for(var i=0;i<3;i++){
if (form.q1[i].checked === true)
{
count1++;
}
}
if(count1!==1){
alert("Please Answer 1st Question");
return false;
}
answer1 = (form.q1.value);
a=Math.floor(answer1);
document.getElementById("result").innerHTML= "The selected values are "+"</br>"+answer1;
}

you should declare a answer variable .and you should access "q1" elements by giving index since you have 3 "q1" elements .basically form.q1 is a object NodeList .you can't get value from object NodeList .so actually in your case you should add brake to for loop and find the clicked radio button index . 您应该声明一个答案变量。由于您有3个“ q1”元素,因此应通过提供索引来访问“ q1”元素。基本上, form.q1是一个object NodeList object NodeList 。您无法从object NodeList获取值。因此实际上如果你应该给循环加刹车并找到单击的单选按钮索引。

you should use 你应该使用

answer1 = form.q1[i].value;

instead of 代替

answer1 = form.q1.value;


explain 说明

form.q1 is a object NodeList so form.q1是一个object NodeList因此

form.q1[0] --> HTMLInputElement so

form.q1[0].value --> is not undefined 

and

function hp(form) {
    var i;
    var answer;
    var count1 = 0,count2 = 0,count3 = 0,count4 = 0,count5 = 0,count6 = 0,count7 = 0,count8 = 0,count9 = 0,count10 = 0, a;

    for (i = 0; i < 3; i++) {
        if (form.q1[i].checked === true) {
            count1++;
            break;
        }
    }
    if (count1 !== 1) {
        alert("Please Answer 1st Question");
        return false;
    }
    answer1 = form.q1[i].value; //error was here .
    a = Math.floor(answer1);
    document.getElementById("result").innerHTML = "The selected values are " + "</br>" + answer1;
}

fixed code . 固定代码。 WORKING DEMO http://jsfiddle.net/madhawa11111/3rywkdvf/ 工作演示http://jsfiddle.net/madhawa11111/3rywkdvf/

 function hp(form) { var i; var answer; var count1 = 0,count2 = 0,count3 = 0,count4 = 0,count5 = 0,count6 = 0,count7 = 0,count8 = 0,count9 = 0,count10 = 0, a; for (i = 0; i < 3; i++) { if (form.q1[i].checked === true) { count1++; break; } } if (count1 !== 1) { alert("Please Answer 1st Question"); return false; } answer1 = form.q1[i].value; //error was here . a = Math.floor(answer1); document.getElementById("result").innerHTML = "The selected values are " + "</br>" + answer1; } 

if it worked in google chorm that's because browsers ignore some errors. 如果它在Google chorm中起作用,那是因为浏览器会忽略一些错误。

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

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