2012-01-11 4 views
0

몇 가지 필수 입력란이있는 양식이 있습니다. 모두 드롭 다운을 제외하고 작동합니다.필수 드롭 다운 문제

<select name="country" id="country"> 
         <option value="" disabled="disabled">Please select your country</option> 
         <option value="United Kingdom">United Kingdom</option> 
         <option value="United States">United States</option> 
... 

내가 사용자를 만들기 위해 실종 무엇 :

function formCheck(formobj){ 
    // Enter name of mandatory fields 
    var fieldRequired = Array("name", "country", "email", "tel"); 
    // Enter field description to appear in the dialog box 
    var fieldDescription = Array("Name", "Country", "Email", "Telephone"); 
    // dialog message 
    var alertMsg = "Please complete the following fields:\n"; 

    var l_Msg = alertMsg.length; 

    for (var i = 0; i < fieldRequired.length; i++){ 
     var obj = formobj.elements[fieldRequired[i]]; 
     if (obj){ 
      switch(obj.type){ 
      case "select-one": 
       if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "select-multiple": 
       if (obj.selectedIndex == -1){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      case "text": 
      case "textarea": 
       if (obj.value == "" || obj.value == null){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
       break; 
      default: 
      } 
      if (obj.type == undefined){ 
       var blnchecked = false; 
       for (var j = 0; j < obj.length; j++){ 
        if (obj[j].checked){ 
         blnchecked = true; 
        } 
       } 
       if (!blnchecked){ 
        alertMsg += " - " + fieldDescription[i] + "\n"; 
       } 
      } 
     } 
    } 

    if (alertMsg.length == l_Msg){ 
     return true; 
    }else{ 
     alert(alertMsg); 
     return false; 
    } 
} 

이 내가 드롭 다운을 위해 사용하고 코드는 다음과 같습니다

나는 필드를 확인하려면이 코드를 사용하고 있습니다 국가를 선택하십시오. "국가를 선택하십시오."라고 표시하고 아무 것도 반환하지 않습니다.

+0

바퀴를 재발 명하기보다는 jQuery와 같은 자바 스크립트 라이브러리를 사용하는 것이 좋습니다. 브라우저 간 차이점을 처리하는 견고하고 견고한 코드를 얻을 수 있습니다. – RedFilter

+0

@RedFilter : 전체 코드 블록을 10 LLOC로 줄일 수 있습니다. –

답변

1

. 로 변경합니다 :

obj.selectedIndex == 0 
+0

위대한, 그 완벽하게, 고마워. – Rob

1

HTML의 selectedIndex 속성은 결코 것 -1 당신이 기다리고있어 수단 0 (http://www.w3schools.com/jsref/prop_select_selectedindex.asp)

에서 시작한다.

인덱스를 0 이상으로 비교해야하며 값을 비교하고 빈 문자열이면 오류 메시지를 표시해야합니다.

지금은 옵션의 value 속성이 아닌 텍스트 속성을 비교하고 있습니다.

obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "" 

이 true로 평가합니다 :이 코드는 항상 true로 될 것이다 예를 들어

0

이 줄은 범인이 될 수 있습니다

if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "") ... 

최초로 확인을 선택한 지수는 -1, 또는 선택한 항목의 text가 비어 있는지합니다. "Please select ..."를 기본값으로 사용한다고 가정 할 때 항상 선택한 항목에 text이 표시됩니다. 이 옵션은 value이 아니며 text 속성이 아닙니다. 그래서 나는 당신이 틀린 재산과 비교하고 있다고 생각합니다. 또한 첫 번째 요소의 인덱스는 0이므로 인덱스 비교가 잘못되었습니다.

로 변경합니다 :

if (obj.selectedIndex < 1 || obj.options[obj.selectedIndex].value == "") ... 
+0

여전히 필수 메시지를 표시하거나 값을 표시하지 않았습니다. – Rob

0

을는 SELECT의 첫 번째 항목은 selectedIndex의 첫 번째 비 장애인 옵션, -1 예상하지 동일합니다 사용할 수 경우.