2013-08-31 6 views
2

벨로우즈 양식 유효성 검사 스크립트를 만들었지 만 작동하지 않습니다. "선택한 항목이 없습니다"라는 메시지가 나타납니다. 이 문제를 해결하는 데 도움을주십시오.자바 스크립트 유효성 검사가 작동하지 않습니다.

내 코드 :

<script type="text/javascript"> 
function ValidateSchool(form){ 
    var cat_school = document.getElementsByName('school[]');  

    for (i = 0; i < cat_school.length; i++){ 
     if (cat_school[i].checked == true){ 
      return true; 
     }else{ 
      alert('No item selected'); 
      return false; 
     } 
    } 
} 
</script> 

<form name="frm" method="post" action="#" onsubmit="return ValidateSchool(this)"> 
    <div class="TopAlphaWrapper"> 
    <div id="topMainAlpha"> 
     <div id="TopAlphaOneLeft"> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Admiralty Primary School" style="display: block;"> 
      <label id="lbl_type0" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.442917,103.799744" type="checkbox"> 
      Admiralty Primary School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Admiralty Secondary School" style="display: block;"> 
      <label id="lbl_type1" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.446367,103.801608" type="checkbox"> 
      Admiralty Secondary School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Advent Learning" style="display: block;"> 
      <label id="lbl_type2" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.324952,103.851188" type="checkbox"> 
      Advent Learning</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="AEC Business School" style="display: block;"> 
      <label id="lbl_type3" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.282717,103.818904" type="checkbox"> 
      AEC Business School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Ahmad Ibrahim Primary School " style="display: block;"> 
      <label id="lbl_type4" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.43367,103.832723" type="checkbox"> 
      Ahmad Ibrahim Primary School </label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Ahmad Ibrahim Secondary School" style="display: block;"> 
      <label id="lbl_type5" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.436482,103.829649" type="checkbox"> 
      Ahmad Ibrahim Secondary School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Ai Tong School" style="display: block;"> 
      <label id="lbl_type6" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.360415,103.832615" type="checkbox"> 
      Ai Tong School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Al-Mubarakah Tuition Centre" style="display: block;"> 
      <label id="lbl_type7" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.352244,103.954274" type="checkbox"> 
      Al-Mubarakah Tuition Centre</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Alps Academia" style="display: block;"> 
      <label id="lbl_type8" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.33079,103.9485" type="checkbox"> 
      Alps Academia</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="American College" style="display: block;"> 
      <label id="lbl_type9" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.280777,103.805617" type="checkbox"> 
      American College</label> 
     </div> 
     </div> 
    </div> 
    </div> 

    <input type="submit" name="submit" value="Submit" /> 
</form> 

어떤 아이디어 나 제안? 감사. 내가 이해할 수있는 최소한 하나 개의 항목이 체크되어 있는지 여부를 확인하려면에서

+0

경우 (cat_school [I] .checked를 = 시도 == "checked") –

+0

여전히 아이템을 선택하지 않았습니다 :( – Manan

+0

getElementsByName ('school')? '학교'대신에? – user1600124

답변

4

유효성 검사 논리는 결함이 보인다

function ValidateSchool(form) { 
    var cat_school = document.getElementsByName('school[]'); 

    var valid = false; 
    for (i = 0; i < cat_school.length; i++) { 
     if (cat_school[i].checked == true) { 
      valid = true; 
      break; 
     } 
    } 

    if (!valid) { 
     alert('No item selected'); 
    } 

    return valid; 
} 

문제는 첫 번째 체크 박스 당신은 선택하지 않은 경우 로직,이다 경고를 표시하고 다른 항목이 선택되어 있는지 여부를 확인하는 대신 목록의 나머지 통해 반복의 false를 반환하는 것은

0

이 시도 : -

<script type="text/javascript"> 
function ValidateSchool(form) { 
    var cat_school = document.getElementsByName('school[]'); 

    var valid = false; 
    for (i = 0; i < cat_school.length; i++) { 
     if (cat_school[i].checked == true) { 
      valid = true; 
      break; 
     } 
    } 

    if (!valid) { 
     alert('No item selected'); 
    } 

    return valid; 
} 
</script> 

<form name="frm" method="post" action="#" onsubmit="return ValidateSchool(this)"> 
    <div class="TopAlphaWrapper"> 
    <div id="topMainAlpha"> 
     <div id="TopAlphaOneLeft"> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Admiralty Primary School" style="display: block;"> 
      <label id="lbl_type0" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.442917,103.799744" type="checkbox"> 
      Admiralty Primary School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Admiralty Secondary School" style="display: block;"> 
      <label id="lbl_type1" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.446367,103.801608" type="checkbox"> 
      Admiralty Secondary School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Advent Learning" style="display: block;"> 
      <label id="lbl_type2" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.324952,103.851188" type="checkbox"> 
      Advent Learning</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="AEC Business School" style="display: block;"> 
      <label id="lbl_type3" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.282717,103.818904" type="checkbox"> 
      AEC Business School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Ahmad Ibrahim Primary School " style="display: block;"> 
      <label id="lbl_type4" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.43367,103.832723" type="checkbox"> 
      Ahmad Ibrahim Primary School </label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Ahmad Ibrahim Secondary School" style="display: block;"> 
      <label id="lbl_type5" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.436482,103.829649" type="checkbox"> 
      Ahmad Ibrahim Secondary School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Ai Tong School" style="display: block;"> 
      <label id="lbl_type6" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.360415,103.832615" type="checkbox"> 
      Ai Tong School</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Al-Mubarakah Tuition Centre" style="display: block;"> 
      <label id="lbl_type7" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.352244,103.954274" type="checkbox"> 
      Al-Mubarakah Tuition Centre</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="Alps Academia" style="display: block;"> 
      <label id="lbl_type8" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.33079,103.9485" type="checkbox"> 
      Alps Academia</label> 
     </div> 
     <div class="email_alert_checkbx_list filter-div ln-a" data-filter="American College" style="display: block;"> 
      <label id="lbl_type9" class="label_check c_off" name="school" style="width:230px !important;"> 
      <input name="school[]" class="chkBX" value="1.280777,103.805617" type="checkbox"> 
      American College</label> 
     </div> 
     </div> 
    </div> 
    </div> 

    <input type="submit" name="submit" value="Submit" /> 
</form> 

가 작동 희망 :) 단지 return 문은 문제 먹으 렴 실행됩니다 체크 박스의 첫 번째 값에 대한 코드에서

0

fiddle

function ValidateSchool(form){ 
var cat_school = document.getElementsByName('school[]'); 
var j=0; 

for (i = 0; i < cat_school.length; i++){ 

    if (cat_school[i].checked){ 
     j++; 
    } 
} 
if(j>0){ 
    alert("item selected"); 
    return true; 
} 
else{ 
    alert("select atleast one value"); 
    return false; 
} 

}

관련 문제