몇 가지 필수 입력란이있는 양식이 있습니다. 모두 드롭 다운을 제외하고 작동합니다.필수 드롭 다운 문제
<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;
}
}
이 내가 드롭 다운을 위해 사용하고 코드는 다음과 같습니다
나는 필드를 확인하려면이 코드를 사용하고 있습니다 국가를 선택하십시오. "국가를 선택하십시오."라고 표시하고 아무 것도 반환하지 않습니다.
바퀴를 재발 명하기보다는 jQuery와 같은 자바 스크립트 라이브러리를 사용하는 것이 좋습니다. 브라우저 간 차이점을 처리하는 견고하고 견고한 코드를 얻을 수 있습니다. – RedFilter
@RedFilter : 전체 코드 블록을 10 LLOC로 줄일 수 있습니다. –