我想驗證一個窗體有多個複選框,並且當「other」複選框被選中並且「other_text」輸入沒有任何內容時。我需要輸入錯誤才能輸入其他文本。這裏是目前的驗證,不適用於我的「其他」複選框。javascript jquery表單驗證
}else if(question_pos==24){
if((!$('#followup_six_45_physician').prop('checked') && !$('#followup_six_45_pharm').prop('checked') && !$('#followup_six_45_nurse').prop('checked') && !$('#followup_six_45_none').prop('checked') && !$('#followup_six_45_other').prop('checked')) ||
($('#followup_six_45_other').prop('checked') && $('#followup_six_45_other_text').val() == "")){
if(($('#followup_six_45_other').prop('checked') && $('#followup_six_45_other_text').val() == "")){
alert("You selected \"Other\" for race, please fill in what other race you consider yourself to be.");
return false;
}else{
alert("Please select an answer.");
return false;
}
}else{
question_pos+=1;
showContent(question_pos,"right");
progress(93, $('#progressBar'));
return true;
}
這是問題...
if(($('#followup_six_45_other').prop('checked') && $('#followup_six_45_other_text').val() == "")){
alert("You selected \"Other\" for race, please fill in what other race you consider yourself to be.");
return false;
這裏是HTML
<div id="area24">
<div id="bl_blue">
<form name="form24" action="handler_2.jsp" onsubmit="return verifyIt();" method="post" style="padding-bottom:20px;">
<input type="hidden" id="from" name="from" value="baseline_01" />
<input type="hidden" id="direction" name="direction" value="" />
<h3>Advice from:</h3>
<table class="screening" width="100%" cellspacing="5px">
<tr>
<td width="8%" align="right"><input name="followup_six_45_physician" id="followup_six_45_physician" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_physician")!=null && session.getAttribute("followup_six_45_physician").equals("1"))?"checked":""%>/></td>
<td width="92%"><label for="followup_six_45_physician">a physician</label></td>
</tr>
<tr>
<td width="8%" align="right"><input name="followup_six_45_pharm" id="followup_six_45_pharm" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_pharm")!=null && session.getAttribute("followup_six_45_pharm").equals("1"))?"checked":""%>/></td>
<td width="92%"><label for="followup_six_45_pharm">a pharmicist</label></td>
</tr>
<tr>
<td width="8%" align="right"><input name="followup_six_45_nurse" id="followup_six_45_nurse" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_nurse")!=null && session.getAttribute("followup_six_45_nurse").equals("1"))?"checked":""%>/></td>
<td width="92%"><label for="followup_six_45_nurse">a nurse</label></td>
</tr>
<tr>
<td width="8%" align="right"><input name="followup_six_45_other" id="followup_six_45_other" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_other")!=null && session.getAttribute("followup_six_45_other").equals("1"))?"checked":""%>/></td>
<td width="92%"><label for="followup_six_45_other">Other:</label> <input name="followup_six_45_other_text" type="text" value="<%=session.getAttribute("followup_six_45_other_text")==null?"":session.getAttribute("followup_six_45_other_text")%>"/></td>
</tr>
<tr>
<td width="8%" align="right"><input name="followup_six_45_none" id="followup_six_45_none" type="checkbox" value="1" <%=(session.getAttribute("followup_six_45_none")!=null && session.getAttribute("followup_six_45_none").equals("1"))?"checked":""%>/></td>
<td width="92%"><label for="followup_six_45_none">I used none of these</label></td>
</tr>
</table>
</form>
</div></div>
到目前爲止,我得到當其他複選框被選中一個錯誤,但是當我添加文本到輸入,錯誤仍然會觸發。我不知道該怎麼做才能讓錯誤只在「followup_six_45_other_text」中沒有文本並且選中「followup_six_45_other」時觸發。
任何幫助將不勝感激,因爲我現在完全卡住了。
看看你能不能創建在http://jsfiddle.net簡化演示 – chrx
你應該使用[jQuery驗證插件(http://bassistance.de/jquery-plugins/jquery-plugin-validation/ )。它可以很容易地處理這種驗證 – Ejaz
您可以共享HTML嗎?將有助於瞭解div結構/ id。 – Bulbasaur