2015-09-28 25 views
1

什么是可能的解决方案,让JavaScript读取我所有的条件,并显示所有发现的是真的?JavaScript不读取我所有的条件

这里是我的代码:

if (document.getElementById('chk_q1').checked == true) { 
 
    if (document.getElementById('chk_c1').checked == false) { 
 
     alert('You forgot to check the Competency.') 
 
     location.href('index.html'); 
 
    }; 
 
    
 
     alert('Correct.'); 
 
} 
 
if (document.getElementById('chk_q2').checked == true) { 
 
    if (document.getElementById('chk_c1').checked == false) { 
 
     alert('You forgot to check the Competency.') 
 
     location.href('index.html'); 
 
    }; 
 
    
 
     alert('Correct.'); 
 
} 
 
if (document.getElementById('chk_q3').checked == true) { 
 
    if (document.getElementById('chk_c1').checked == false) { 
 
     alert('You forgot to check the Competency.') 
 
     location.href('index.html'); 
 
    }; 
 
    
 
     alert('Correct.'); 
 
}
<form> 
 
    <button type="button" class="btn btn-default btn-sm" onclick="javascript:demoPDF()" id="final">Finalized Customized Interview Form</button> 
 
    <button type="button" class="btn btn-success btn-sm" onClick="actions.save()">Save</button> 
 
    <button type="reset" class="btn btn-warning btn-sm" id="uncheckall">Reset Form</button> 
 
    <br> 
 
    <br> 
 
    <br> 
 
    <!-- START OF INTERVIEW INFO --> 
 
    <table style="width:100%" id="tbl_info"> 
 
    <tr> 
 
     <th colspan="4">Interview Information</th> 
 
    </tr> 
 

 
    <tr> 
 
     <td>Hiring Manager Name:</td> 
 

 
     </td> 
 
     <td> 
 
     <input type="text" name="name" id="HMname" placeholder="Type manager's name here..." style="width:205px"> 
 
     </td> 
 
     </td> 
 
     <td>Recruiter Name:</td> 
 
     </td> 
 
     <td> 
 
     <input type="text" name="HMN" id="Rname" placeholder="Type position title here..." style="width:205px"> 
 
     </td> 
 

 
    </tr> 
 

 
    <tr> 
 
     <td>Position Title:</td> 
 

 
     </td> 
 
     <td> 
 
     <input type="text" name="HMN" id="Ptitle" placeholder="Type recruiter's name here..." style="width:205px"> 
 
     </td> 
 
     </td> 
 
     <td>Position Code:</td> 
 
     </td> 
 
     <td> 
 
     <input type="text" name="HMN" id="Pcode" placeholder="Type position code here..." style="width:205px"> 
 
     </td> 
 

 
    </tr> 
 

 

 
    <tr> 
 
     <td>Candidate Name:</td> 
 

 
     </td> 
 
     <td>&nbsp</td> 
 
     </td> 
 
     <td>Interview Date:</td> 
 
     </td> 
 
     <td>&nbsp</td> 
 

 
    </tr> 
 
    </table> 
 
    <!-- </form> --> 
 
    <!-- END OF INTERVIEW INFO --> 
 

 
    <!-- STRAT OF COMPETENCY --> 
 

 
    <!-- <form action="CC.php" name="select_form"> --> 
 
    <!-- <button type="button" class="btn btn-default btn-sm" >Finalized Customized Interview Form</button> 
 
\t \t \t \t <button type="button" class="btn btn-success btn-sm" id="uncheckall" name="uncheckall" alt="save as">Save</button> 
 
\t \t \t \t <button type="button" class="btn btn-warning btn-sm" value="reset" id="uncheckall" name="uncheckall" onclick=>Reset Form</button> 
 
\t \t \t \t <br> 
 
\t \t \t \t <br> 
 
\t \t \t \t <br> --> 
 
    <table style="width:100%;margin-top:10px" id="tbl_selection"> 
 
    <tr> 
 
     <th><b>Competency and Select to use for<br> Interview Series</b> 
 
     </th> 
 
     <th><b>Questions</b> 
 
     </th> 
 
     <th><b>Use for Interview Series?</b> 
 
     </th> 
 
    </tr> 
 
    <!-- INNOVATION --> 
 
    <tr style="background-color:#FEE2B8"> 
 
     <td rowspan="4"><b>Balance Focus</b> 
 
     <br> 
 
     <!-- <form> --> 
 
     <input type="checkbox" id="chk_c1" onselect="javascript:select()" class="select">Select 
 
     <br> 
 
     <input type="checkbox" id="not_use" checked>Do not use 
 
     <!-- <br> 
 
\t \t \t \t \t \t <input type="radio" name="not_select" value="not selected"> Do not Select --> 
 
     <!-- </form> --> 
 
     </td> 
 
     <td id="q1" value="lorem">Give me an example of a time where you had to make key decisions in the absence of complete information. How did you recognize when a solution is good enough?</td> 
 
     <td> 
 
     <!-- <form> --> 
 
     <input type="checkbox" id="chk_q1">Select 
 
     <br> 
 
     <input type="checkbox" id="not_use1" checked>Do not use 
 
     <!-- <br> 
 
\t \t \t \t \t \t <input type="radio" name="not_select" value="not selected"> Do not Select --> 
 
     <!-- </form> --> 
 

 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Describe a situation where you were involved in creating alternative business scenarios, prioritized them based on your knowledge of the business, and put a plan in place to enable the strategy.</td> 
 
     <td> 
 
     <!-- <form> --> 
 
     <input type="checkbox" id="chk_q2">Select 
 
     <br> 
 
     <input type="checkbox" id="not_use" checked>Do not use 
 
     <!-- <br> 
 
\t \t \t \t \t \t \t <input type="radio" name="not_select" value="not selected"> Do not Select --> 
 
     <!-- </form> --> 
 
     </td> 
 

 
    </tr> 
 
    <tr style="background-color:#FEE2B8"> 
 
     <td>Tell me about a situation in which you were responsible for a project where you had to satisfy multiple stakeholders with different priorities. How did you balance conflicting demands and arrive at a successful outcome?</td> 
 
     <td> 
 
     <!-- <form> --> 
 
     <input type="checkbox" id="chk_q3">Select 
 
     <br> 
 
     <input type="checkbox" id="not_use" checked>Do not use 
 
     <!-- <br> 
 
\t \t \t \t \t \t \t <input type="radio" name="not_select" value="not selected"> Do not Select --> 
 
     <!-- </form> --> 
 
     </td> 
 
    </tr> 
 
    <tr> 
 
     <td>Tell me about a time when your group was assigned multiple priorities by multiple leaders all stressing the importance of their own projects? How did you prioritize the projects? How did you inform the leaders of their project time frames?</td> 
 
     <td> 
 
     <!-- <form> --> 
 
     <input type="checkbox" id="chk_q4">Select 
 
     <br> 
 
     <input type="checkbox" id="not_use" checked>Do not use 
 
     <!-- <br> 
 
           <input type="radio" name="not_select" value="not selected"> Do not Select --> 
 
     <!-- </form> --> 
 
     </td> 
 
    </tr> 
 
    </table>

这里的问题是,它只是显示的所有内容,如果我只检查的第一个复选框。

问题的任何解决方案?

+0

你能提供你的HTML吗?这将有助于了解你正在尝试做什么。 – Romulo

+0

如果脚本在html载入前运行,那么'getElementById'将返回null,它没有'checked'属性,导致错误被抛出,并且脚本执行停止。 – HJ05

+0

@Buzinas我将这些阅读为不同的ID,因此可能会有不同的值。可怕的命名,但不同。 –

回答

0

你可以使用变量来保存状态,只有在最后告诉他们:

var messages = [], valid = true; 

if (document.getElementById('chk_q1').checked == true) { 
    if (document.getElementById('chk_c1').checked == false) { 
    messages.push('You forgot to check the Competency - C1'); 
    valid = false; 
    } 
    else { 
    messages.push('Correct - C1'); 
    } 
} 

if (document.getElementById('chk_q2').checked == true) { 
    if (document.getElementById('chk_c2').checked == false) { 
    messages.push('You forgot to check the Competency - C2'); 
    valid = false; 
    } 
    else { 
    messages.push('Correct - C2'); 
    } 
} 

if (document.getElementById('chk_q3').checked == true) { 
    if (document.getElementById('chk_c3').checked == false) { 
    messages.push('You forgot to check the Competency - C3'); 
    valid = false; 
    } 
    else { 
    messages.push('Correct - C3'); 
    } 
} 

alert(messages.join('\n')); 
if (!valid) 
    location.assign('index.html'); 
+0

谢谢!它真的有帮助,你的JavaScript的人!大声笑:) –

+0

感谢每一个! :) –

+0

@Rainsky如果我的回答对你有用,请点击箭头将它调高,并通过点击upvote/downvote箭头下方的'V'标记为已接受。谢谢! :) – Buzinas

0

的问题可能是在if语句,如果条件被发现是真正的所有其他else if语句将不会执行。你可以在代码块中继续执行其余的代码。 if(condition){ 东西东西 继续 }

+1

这不是如何*继续*的作品。它可以用来短路一个循环,并停止进一步评估当前块中的语句,如* break *,但它保留在块中。请参阅[* MDN继续*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue) – RobG

+0

他的代码中没有else if语句。你在说什么? – Barmar