Jquery切换不正确的元素ID

时间:2016-08-19 17:22:13

标签: javascript jquery html pdf

我有一个多项选择测验,有5个可能的点和一个二进制,是或否答案。

我对jsPDF和html2canvas感到沮丧,所以我做了一个相当低效的解决方法,我希望能为我起作用。

我为每个证书保存了一个单独的pdf,并且可以通过<a href="PDF.pdf" download>访问它们。我知道只需在大多数浏览器上打开PDF,但这实际上或多或少都是我想要的。

我认为我的困难在于我的Jquery。 无论你得到什么分数,它都默认切换第一个链接并给你证明你得到一分的证书,并对次要问题说是。

这是HTML。

<div class="answers">            
    <input type="radio" name="q1" id="q1a"><label for="q1a">option 1</label><br/><br/>          
    <input type="radio" name="q1" id="q1b"><label for="q1b">option 2</label><br/><br/>            

<div class="answers">            
    <input type="radio" name="q2" id="q2a"><label for="q2a">option 1</label><br/><br/>          
    <input type="radio" name="q2" id="q2b"><label for="q2b">option 2</label><br/><br/>            

<div class="answers">            
    <input type="radio" name="q3" id="q3a"><label for="q3a">option 1</label><br/><br/>          
    <input type="radio" name="q3" id="q3b"><label for="q3b">option 2</label><br/><br/>            

<div class="answers">            
    <input type="radio" name="q4" id="q4a"><label for="q4a">option 1</label><br/><br/>          
    <input type="radio" name="q4" id="q4b"><label for="q4b">option 2</label><br/><br/>            

<div class="answers">            
    <input type="radio" name="q5" id="q5a"><label for="q5a">option 1</label><br/><br/>          
    <input type="radio" name="q5" id="q5b"><label for="q5b">option 2</label><br/><br/>            

<div class="answers">            
    <input type="radio" name="qx" id="qxa"><label for="qxa">Praise the sun!</label><br/><br/>          
    <input type="radio" name="qx" id="qxb"><label for="qxb">Nope.</label><br/><br/>            

<p id="endButton"></p>

<button type="button" onclick="getScore();">Finish</button>

<br/>
<div class="cert" id="cert0n">
<a href="Cert 0n.pdf">Download my certificate!</a>
</div>
<div class="cert" id="cert0y">
<a href="Cert 0y.pdf">Download my certificate!</a>
</div>

<div class="cert" id="cert1n">
<a href="Cert 1n.pdf">Download my certificate!</a>
</div>
<div class="cert" id="cert1y">
<a href="Cert 1y.pdf">Download my certificate!</a>
</div>

<div class="cert" id="cert2n">
<a href="Cert 2n.pdf">Download my certificate!</a>
</div>
<div class="cert" id="cert2y">
<a href="Cert 2y.pdf">Download my certificate!</a>
</div>

<div class="cert" id="cert3n">
<a href="Cert 3n.pdf">Download my certificate!</a>
</div>
<div class="cert" id="cert3y">
<a href="Cert 3y.pdf">Download my certificate!</a>
</div>

<div class="cert" id="cert4n">
<a href="Cert 4n.pdf">Download my certificate!</a>
</div>
<div class="cert" id="cert4y">
<a href="Cert 4y.pdf">Download my certificate!</a>
</div>

<div class="cert" id="cert5n">
<a href="Cert 5n.pdf">Download my certificate!</a>
</div>
<div class="cert" id="cert5y">
<a href="Cert 5y.pdf">Download my certificate!</a>
</div>

“cert”类设置为display: none,以便切换允许它显示。

这是Javascript:

var totscore = 0;
var noanswer = 0;

var scores1 = [0, 1];
var scores2 = [0, 1];
var scores3 = [0, 1];
var scores4 = [0, 1];
var scores5 = [0, 1];

var praiseTheSun = 0

if(document.getElementById("q1a").checked){
    totscore += scores1[0];
} else if(document.getElementById("q1b").checked){
    totscore += scores1[1];
} else noAnswer = 1;

if(document.getElementById("q2a").checked){
    totscore += scores1[0];
} else if(document.getElementById("q2b").checked){
    totscore += scores1[1];
} else noAnswer = 1;

if(document.getElementById("q3a").checked){
    totscore += scores1[0];
} else if(document.getElementById("q3b").checked){
    totscore += scores1[1];
} else noAnswer = 1;

if(document.getElementById("q4a").checked){
    totscore += scores1[0];
} else if(document.getElementById("q4b").checked){
    totscore += scores1[1];
} else noAnswer = 1;

if(document.getElementById("q5a").checked){
    totscore += scores1[0];
} else if(document.getElementById("q5b").checked){
    totscore += scores1[1];
} else noAnswer = 1;

 if(document.getElementById("qxa").checked){
    praiseTheSun == 1
} else if(document.getElementById("qxb").checked){
    praiseTheSun == 0  
}else noAnswer = 1;

    if(noAnswer == 1){
    alert("At least one question was left unanswered above. Please ensure that all answers are complete.");
} else if(noAnswer == 0){
    alert("Your score is " + totscore + ".");
    if((totscore = 0) && (praiseTheSun = 1)){
        $("#cert0y").toggle();
    }else if((totscore = 0) && (praiseTheSun = 0)){
        $("#cert0n").toggle();

    }else if((totscore = 1) && (praiseTheSun = 1)){
        $("#cert1y").toggle();
    }else if((totscore = 1) && (praiseTheSun = 0)){
        $("#cert1n").toggle();

    }else if((totscore = 2) && (praiseTheSun = 1)){
        $("#cert2y").toggle();
    }else if((totscore = 2) && (praiseTheSun = 0)){
        $("#cert2n").toggle();

    }else if((totscore = 3) && (praiseTheSun = 1)){
        $("#cert3y").toggle();
    }else if((totscore = 3) && (praiseTheSun = 0)){
        $("#cert3n").toggle();

    }else if((totscore = 4) && (praiseTheSun = 1)){
        $("#cert4y").toggle();
    }else if((totscore = 4) && (praiseTheSun = 0)){
        $("#cert4n").toggle();

    }else if((totscore = 5) && (praiseTheSun = 1)){
        $("#cert5y").toggle();
    }else if((totscore = 5) && (praiseTheSun = 0)){
        $("#cert5n").toggle();
    }

我无法理解为什么,无论得到什么分数,都默认为cert1y。

1 个答案:

答案 0 :(得分:0)

正如一些评论指出的那样,你在if语句中分配变量(=)而不是比较它们(==)。所以这就是为什么cert1y被选中的原因:

第一个if将totscore设置为0并将theTheSum设置为1,然后比较它们:(0&amp;&amp; 1)为false,依此类推。否则。

第二个if将totscore设置为0并将theTheSum设为0,然后比较它们:(0&amp;&amp; 0)为false,依此类推。否则。

第三个如果将totscore设置为1并将theThe赞美为1,然后将它们进行比较:(1&amp;&amp; 1)为真,那么cert1y将被切换。

如果您在任何现代浏览器中学习如何使用脚本调试器,您可以单步执行代码并实时查看。