2013-04-17 4 views
0

Я пытаюсь проверить форму, которая имеет несколько флажков, и когда установлен флажок «другой», а в поле «other_text» ничего нет. Мне нужна ошибка, чтобы я попросил ввести другой текст. вот текущая проверка, которая не работает с моим «другим» флажком.Проверка формы jQuery javascript

}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».

Любая помощь будет оценена, поскольку я полностью застрял прямо сейчас.

+1

Смотрите, если вы можете создать упрощенную демо на http://jsfiddle.net – chrx

+0

вы должны использовать [JQuery плагин проверки] (http://bassistance.de/jquery-plugins/jquery-plugin-validation/). Он может легко позаботиться о такой проверке – Ejaz

+0

Можете ли вы поделиться HTML? Помогло бы понять структуру div/ids. – Bulbasaur

ответ

1

Похоже, что вы запрашиваете идентификатор для followup_six_45_other_text, но идентификатор фактически не установлен (имя установлено, id нет, вы запрашиваете по id).

 <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> 
+0

О, черт! Я не могу поверить, что это так просто, СПАСИБО! – user2089255