2017-03-01 36 views
0

无效无效我试图做一个网站,有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> 

不管我做什么或不输入任何框,它出现无效,不会进入下一个屏幕。

+0

为什么要检查'$(“#question1”)。prop('defaultValue')'默认值。它总是会给出'value =“问题”'使情况总是如此。这就是为什么'if'语句正在执行。 – Ayush

回答

1

您的if条件存在问题。

if ($("#question1").val() === "" || $("#question1").prop('defaultValue') === $("#question1").val()) { 
       alert("Please fill out all fields"); 
       // since there is invalid input returning false will keep us from navigating to the new form 
       return false; 
    } else { 
       // input is valid so we'll navigate to the new form 
      alert('Okay'); 

    } 

使用这样的事情,因为$("#question1").prop('defaultValue')将返回input元素定义为value="Question"的默认值。所以,第二个条件永远是真的。这就是为什么它总是会给出错误信息。要么删除该条件,要么更新条件。 我希望它可以帮助你。

0

您的“if”条件存在问题。在JS中使用 ,如果你想要有两个条件的“if”语句,你应该把整个条件放在每个“||”运算符:

if (x == y || x == z) 
相关问题