У меня есть элемент select на странице формы, и я хотел бы, чтобы внутренний html-ярлык был изменен, если пользователь пытается отправить без выбора. Вот HTML.
<div class="form-group">
<label for="FamilySize" name = "FamSizeLable">How Many in Your Household</label>
<select class="form-control" name = "FamilySize" id="FamilySize">
<option disabled selected value="">Size</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>Good god ya'll</option>
</select>
</div>
Я пытался получить значение несколькими различными способами.
else if(!document.getElementById("FamilySize").value){
document.getElementByName("FamSizeLabel").innerHTML = "***You Must Select a Family Size***";
document.getElementById("FamSizeLabel").style.color = "red";
return false;
}
и
else if(!document.querySelector('[id = "FamilySize"]').value){
document.getElementByName("FamSizeLabel").innerHTML = "***You Must Select a Family Size***";
document.getElementById("FamSizeLabel").style.color = "red";
return false;
}
и
else if(document.querySelector('[id = "FamilySize"]').value==null){
document.getElementByName("FamSizeLabel").innerHTML = "***You Must Select a Family Size***";
document.getElementById("FamSizeLabel").style.color = "red";
return false;
}
и
else if(document.querySelector('[id = "FamilySize"]').value==""){
document.getElementByName("FamSizeLabel").innerHTML = "***You Must Select a Family Size***";
document.getElementById("FamSizeLabel").style.color = "red";
return false;
}
Я также попытался использовать console.log в представлении разработчика, чтобы увидеть, что мне не хватает. Тем не менее, я довольно новичок в JavaScript и не могу его закрепить. Форма все еще отправляется, когда FamilySize не выбран. Я могу поймать эту сторону сервера, но я пытаюсь улучшить JavaScript, поэтому хочу это понять / понять.
Редактировать (полный код страницы указан ниже):
{% extends "layout.html" %}
{% block main %}
<!--form action tells this form what route to post the data to. DOH! -->
<form action="/form" method="post">
<div class="form-group">
<!--for="FormControlInput1"This was in the EmailLablel label not sure what it does-->
<label id = "EmailLabel" >Email address</label>
<input type="email" class="form-control" name = "Email" autocomplete = "off" autofocus id="FormControlInput1" placeholder="name@example.com">
</div>
<div class="form-group">
<label id = "Fname" >First Name</label>
<input type="text" class="form-control" name = "Fname" autocomplete = "off" id="FnameFormControlInput" placeholder="John">
</div>
<div class="form-group">
<label id = "Lname" >Last Name</label>
<input type="text" class="form-control" name ="Lname" autocomplete = "off" id="LnameFormControlInput" placeholder="Doe">
</div>
<div class="form-group">
<label for="FamilySize" name = "FamSizeLable">How Many in Your Household</label>
<select class="form-control" name = "FamilySize" id="FamilySize">
<option disabled selected value="">Size</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>Good god ya'll</option>
</select>
</div>
<div class="form-group">
<label for="AgeGroup">Select Age Group</label>
<select multiple class="form-control" name = "AgeGroup" id="AgeGroup">
<option>0-15</option>
<option>16-24</option>
<option>25-36</option>
<option>37-45</option>
<option>45-Dead</option>
</select>
</div>
<div class="form-group">
<label for="Comments">Comments</label>
<textarea class="form-control" name = "Comments" id="Comments" rows="3"></textarea>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
<script>
document.querySelector('form').onsubmit=function(){
if(!document.querySelector('[type = "email"]').value){
document.getElementById("EmailLabel").innerHTML = "***You Must Provide an Email address***";
document.getElementById("EmailLabel").style.color = "red";
return false;
}
else if(!document.querySelector('[id = "FnameFormControlInput"]').value){
document.getElementById("Fname").innerHTML = "***You Must Provide a First Name***";
document.getElementById("Fname").style.color = "red";
return false;
}
else if(!document.querySelector('[id = "LnameFormControlInput"]').value){
document.getElementById("Lname").innerHTML = "***You Must Provide a Last Name***";
document.getElementById("Lname").style.color = "red";
return false;
}
// //Here we have to use label for family size because there is no text on the control itself
else if(!document.getElementById("FamilySize").value){
document.getElementByName("FamSizeLabel").innerHTML = "***You Must Select a Family Size***";
document.getElementById("FamSizeLabel").style.color = "red";
console.log(document.querySelector('[id = "FamilySize"]').value);
return false;
}
else if(!document.querySelector('[id = "AgeGroup"]').value){
document.getElementById("AgeGroup").innerHTML = "***You Select Your Age Group***";
document.getElementById("AgeGroup").style.color = "red";
return false;
}
alert(document.querySelector('[id = "FamilySize"]').value);
return true;
};
</script>
{% endblock %}