0

我正在尝试验证一个具有多个复选框的表单,并且在选中“其他”复选框时,“其他_text”输入中没有任何内容。我需要一个错误来触发要求输入其他文本。这是不适用于我的“其他”复选框的当前验证。

}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”时触发。

任何帮助将不胜感激,因为我现在完全陷入困境。

4

1 回答 1

1

看起来您正在通过 id 查询 followup_six_45_other_text,但实际上并未设置 id(名称已设置,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>
于 2013-04-17T18:15:08.570 回答