无效无效我试图做一个网站,有3个字段默认值。如果所有字段都填写完毕,我想要一个提醒,弹出“确定”,它将带您到一个新的网站。如果它们留空或者没有回答,我想要一个提示来填写所有的字段。问题是,无论在田地里有什么,我都会得到“再试一次”的警报。需要一个弹出窗口,说是否有效的表单 - 无论如何
我的JS -
$(function() {
\t \t // when the text field gets focus it gets ride of the default value
\t \t //:text makes the browser check for ALL text fields
\t \t $(':text').focus(function() {
\t \t \t console.log('got focus');
\t \t \t var field = $(this);
\t \t \t //basically asks - is it blank? if not, put default value in
\t \t \t if (field.val()==field.prop('defaultValue')) {
\t \t \t \t field.val('');
\t \t \t }
\t \t });
\t \t
\t \t $(':text').blur(function() {
\t \t \t console.log('lost focus');
\t \t \t var field = $(this);
\t \t \t
\t \t \t //basically asks - is it blank? if not, put default value in
\t \t \t if (field.val()=='') {
\t \t \t \t field.val(field.prop('defaultValue')); \t \t \t \t
\t \t \t }
\t \t });
\t
\t \t //not working....
\t \t $("#questionform").submit(function() {
\t \t \t
\t \t \t if ($("#question1").val() === "" || $("#question1").prop('defaultValue')) {
\t \t \t \t alert("Please fill out all fields");
\t \t \t \t // since there is invalid input returning false will keep us from navigating to the new form
\t \t \t \t return false;
\t \t \t }
\t \t \t else {
\t \t \t \t // input is valid so we'll navigate to the new form
\t \t \t \t alert('Okay');
\t \t \t \t
\t \t \t }
\t \t \t
\t \t \t \t \t if ($("#question2").val() === "" || $("#question2").prop('defaultValue')) {
\t \t \t \t alert("Please fill out all fields");
\t \t \t \t // since there is invalid input returning false will keep us from navigating to the new form
\t \t \t \t return false;
\t \t \t }
\t \t \t else {
\t \t \t \t // input is valid so we'll navigate to the new form
\t \t \t \t alert('Okay');
\t \t \t \t
\t \t \t }
\t \t \t
\t \t \t \t \t if ($("#question3").val() === "" || $("#question3").prop('defaultValue')) {
\t \t \t \t alert("Please fill out all fields");
\t \t \t \t // since there is invalid input returning false will keep us from navigating to the new form
\t \t \t \t return false;
\t \t \t }
\t \t \t else {
\t \t \t \t // input is valid so we'll navigate to the new form
\t \t \t \t alert('Okay');
\t \t \t \t
\t \t \t }
\t \t }); \t \t
\t \t
\t \t \t \t \t \t
\t }); // end ready
我的HTML -
<form action="success.html" id="questionform">
<label for="question1" class="label">Enter your first name</label>
<br>
<input type="text" value="Question" id="question1">
<br>
<label for="question2" class="label">Enter your last name</label>
<br>
<input type="text" value="Question" id="question2">
<br>
<label for="question3" class="label">Enter your favorite color</label>
<br>
<input type="text" value="Question" id="question3">
<br>
<input type='submit' value='Submit' name="submit" id='submitme'>
</form>
<p id="result"></p>
不管我做什么或不输入任何框,它出现无效,不会进入下一个屏幕。
为什么要检查'$(“#question1”)。prop('defaultValue')'默认值。它总是会给出'value =“问题”'使情况总是如此。这就是为什么'if'语句正在执行。 – Ayush