这里提交的功能不工作是我的HTML代码:当我按下空形式
<form name="myForm" action="userreview.html" onsubmit="return !!(tosubmit() & validateForm())" >
<div style="padding-bottom: 18px;">First Name:<span style="color: red;"> </span><br/></div>
<input type="text" name="firstname" id="firstname">
<br>
<div style="padding-bottom: 18px;">Last Name:<span style="color: red;"> </span><br/></div>
<input type="text" name="lastname" id="lastname">
<br>
<div style="padding-bottom: 18px;">Review:<span style="color: red;"> </span><br/></div>
<textarea id="data_8" ${readonly} name="data_8" style="width : 450px;" rows="10" class="form-control"></textarea>
<nav>
<fieldset class="rating" name="rate1">
<legend>Please rate:</legend>
<input type="radio" id="star5" name="rating" value="5" onchange="test(this)" /><label for="star5" title="Rocks!">★</label>
<input type="radio" id="star4" name="rating" value="4" onchange="test(this)" /><label for="star4" title="Pretty good">★</label>
<input type="radio" id="star3" name="rating" value="3" onchange="test(this)" /><label for="star3" title="Meh">★</label>
<input type="radio" id="star2" name="rating" value="2" onchange="test(this)" /><label for="star2" title="Kinda bad">★</label>
<input type="radio" id="star1" name="rating" value="1" onchange="test(this)" /><label for="star1" title="Sucks big time">★</label>
</fieldset>
</nav>
<div style="padding-bottom: 18px;">Would you recommend this product?<span style="color: red;"> *</span><br/>
<span><input type="radio" id="data_9_0" name="data_9" value="Yes"/> Yes</span><br/>
<span><input type="radio" id="data_9_1" name="data_9" value="No"/> No</span><br/>
<span><input type="radio" id="data_9_2" name="data_9" value="I am not sure"/> I am not sure</span><br/>
</br>
<input type="submit" value="Submit">
</div>
</form>
</div>
这里是我的jQuery代码:
<script>
function tosubmit() {
// Getting the value of your text input
var mytext = document.getElementById("data_8").value;
// Storing the value above into localStorage
localStorage.setItem("mytext", mytext);
localStorage.setItem("stars", stars);
return true;
}
var stars;
function test(myInput)
{
stars = myInput.value;
//alert(stars);
}
function validateForm() {
var x = document.querySelectorAll("#data_8, #rating, #firstname, #lastname");
if (x == "") {
alert("Name must be filled out");
return false;
}
}
</script>
为什么当我按下它不提醒用空表单提交?我犯了什么错误吗?尽管它不会转到其他页面,但它不会提醒用户填写空表单。
有人可以帮助我吗?我做了很长时间。
'x'是一个NodeList而不是字符串。所以它永远不会等于''“'。 querySelectorAll将根据可用性返回NodeList或null。你将不得不遍历元素并单独检查它们的值。 – Rajesh