자바 스크립트로 양식 유효성 검사를 사용하고 있습니다. 제출할 때 유효하지 않은 입력 필드의 배경색이 빨간색으로 바뀝니다. 이 입력란을 채우고 다른 입력란에 입력하면 이전 입력란의 빨간색 배경색이 사라집니다. 지금은 그렇지 않습니다. 다시 제출할 때만 사라집니다. 다른 필드에 타이핑 할 때 bg 색상이 다시 정상적으로 변경되도록하려면 어떻게해야합니까?배경색 입력란을 사용 중지하는 방법
// Return true if the input value is not empty
function isNotEmpty(inputId, errorMsg) {
var inputElement = document.getElementById(inputId);
var errorElement = document.getElementById(inputId + "Error");
var inputValue = inputElement.value.trim();
var isValid = (inputValue.length !== 0); // boolean
showMessage(isValid, inputElement, errorMsg, errorElement);
return isValid;
}
/* If "isValid" is false, print the errorMsg; else, reset to normal display.
* The errorMsg shall be displayed on errorElement if it exists;
* otherwise via an alert().
*/
function showMessage(isValid, inputElement, errorMsg, errorElement) {
if (!isValid) {
// Put up error message on errorElement or via alert()
if (errorElement !== null) {
errorElement.innerHTML = errorMsg;
} else {
alert(errorMsg);
}
// Change "class" of inputElement, so that CSS displays differently
if (inputElement !== null) {
inputElement.className = "error";
inputElement.focus();
}
} else {
// Reset to normal display
if (errorElement !== null) {
errorElement.innerHTML = "";
}
if (inputElement !== null) {
inputElement.className = "";
}
}
}
형태 :
<td>Name<span class="red">*</span></td>
<td><input type="text" id="name" name="firstname"/></td>
<p id="nameError" class="red"> </p>
가 제출
<input type="submit" value="SEND" id="submit"/>
CSS는 : 나는이 B를 시도 : 업데이트
input.error { /* for the error input text fields */
background-color: #fbc0c0;
}
유타는 작동하지 않는 것 같습니다 : 시도 demo
이것은 하나의 단일 입력 ID로 훌륭하게 작동합니다. 하지만 다른 ID를 가진 여러 입력 필드가 있습니다. 모든 분야에서 어떻게 작동시킬 수 있습니까? – nuet
위 질문이 업데이트되었습니다. – nuet