2014-10-23 4 views
0

javascript로 HTML 양식의 유효성을 검사하려고하는데 경고가 표시되지 않아 checkSubmit() 함수가 호출되지 않는다고 생각했습니다. 그러나 기능 시작 부분에 추가 경고를 추가 한 후에는 실제로 실행되지만 첫 번째 조건을 확인한 후에는 더 이상의 경고가 표시되지 않습니다.조건 확인 후 경고 양식이 유효하지 않음

<script language="javascript" type="text/javascript"> 
    function checkSubmit() { 

    alert("Hello from Javascript!"); 
    if (document.getElementById("user").value.toString.length <1) { 
     alert("Please enter a user name."); 
     return false; 
    } 

    if (document.getElementById("location").value.toString.length <1) { 
     alert("Please enter a location."); 
     return false; 
    } 
    if (document.getElementById("depart").value.toString.length <1) { 
     alert("Please enter a department."); 
     return false; 
    } 
    if (document.getElementById("category").value.toString.length <1) { 
     alert("Please enter a problem type."); 
     return false; 
    } 
    if (document.getElementById("info").value.toString.length <1) { 
     alert("Please enter a problem description."); 
     return false; 
    } 
    alert("Hello from Javascript2!"); 
    return true; 
    } 
</script> 

<form name="submit" type="submit" method="POST" onsubmit="return checkSubmit();"> 

    [...] 

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

나는 (내가 돌이켜에서 명백 할 것이다 확신) 실수를하지 않는 한, 거기에 조건이 참 또는 거짓 경우 두 번째 경고에 관계없이, 팝업 안?

+0

콘솔에 오류가보고됩니까? 어쩌면 당신의 컨트롤 중 하나를 찾지 못할 수도 있습니다 ... – Paddy

+0

값이 정의되지 않은 것과 같은 오류가 발생했습니다. –

+0

아하. 첫 번째 입력 상자에 "id ="태그가 없습니다. 그러나 이제는 반대의 문제가 있습니다. ID가 "user"인 상자에 텍스트가있는 경우에도 사용자 이름을 입력하라는 경고가 나타납니다. – CCKx

답변

1

.value은 이미 문자열이 아니므로 toString이 필요하지 않으며 어쨌든 잘못 사용하고 있습니다. .toString()라고해야합니다. 즉, 함수 정의를 반환하므로 .length0이됩니다. 그리고 말했듯이 각 상자에 대해 id 속성을 올바르게 설정해야합니다.

function checkSubmit() { 

    if (document.getElementById("user").value.length < 1) { 
     alert("Please enter a user name."); 
     return false; 
    } 

    if (document.getElementById("location").value.length < 1) { 
     alert("Please enter a location."); 
     return false; 
    } 

    if (document.getElementById("depart").value.length < 1) { 
     alert("Please enter a department."); 
     return false; 
    } 

    if (document.getElementById("category").value.length < 1) { 
     alert("Please enter a problem type."); 
     return false; 
    } 

    if (document.getElementById("info").value.length < 1) { 
     alert("Please enter a problem description."); 
     return false; 
    } 

    return true; 
} 
+0

감사합니다. 이 지금 작동합니다. 이 질문이 페이지가 원래 다른 행동없이 제출/새로 고침하는 것처럼 보이기 때문에 자신의 "onsubmit"이 작동하지 않는다고 생각하는 막 다른 길을가는 다른 사람들에게 도움이되기를 바랍니다. – CCKx

관련 문제