验证表单然后使用jquery显示其他隐藏表单

时间:2013-09-18 07:42:56

标签: javascript jquery html forms validation

我的jquery代码有点问题。我的页面中有两个表单。当用户加载页面时显示另一个表单,另一个表示隐藏(设置为style =“display:none; “)。我想填写第一个表单中的所有细节,然后单击下一步,显示隐藏的表单..问题是,即使第一个表单没有输入,也会在点击“下一个”按钮时显示隐藏表单。所以我我猜我在使用jquery验证代码做错了(我正在使用jQuery Validation插件)..任何帮助都会受到赞赏。谢谢提前

以下是我的代码的样子:          细节

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">    </script>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>

<body>

<div class="item item1">
<form action="index.html" method="get" id = "form1">

<h1>The DETAILS</h1>
<p align="justify">Try our FREE landlord cost calculator and<br>
  discover the real cost of letting your property. <br>
   <input name="name"  type="text" placeholder="your name"class="required" />


  <br>

  <input name="email" type="text" placeholder="your email" class="required" />
  <br>

  <input name="phone" id="phone" type="text" placeholder="your phone no"  class="required" />
  <br>

  <input name="postcode" id="postcode" type="text" placeholder="postcode of property to let" class="required" />
  <br>

  <select name="bedrooms" id="bedrooms" placeholder="bedrooms" class="required" >

   <option value="1">1</option>
   <option value="2" >2</option>
   <option value='' disabled selected="selected">Please Choose</option>
  </select> 
  <br>

  <input name="hearAbout" type="text" placeholder="where did your hear about us?" class="required" />
  <br>

   <button class="submit button1 " id = "next" name="submit" type="submit" value="next"><span class="next ">></span>next</button>
   </p>
   </form>



 </div>

   <script type="text/javascript">
  $(document).ready(function(){
  $("#form1").validate();
  });
  </script>


 <script>
 $("#form1").submit(function() {
  /* AJAX calls and insertion into #form2 */
  $("#form2").show();
  return false;
  });
  </script>   



  <div class="item item2">


 <form action="calculation.php" method="post" id = "form2"style="display:none;"> 
 <h1>The Income</h1>
 <label> Estimated monthly rent:&pound; </label>     <input name="rent" id="rent"    type="text" /><hr>
 THE COSTS            STEP 3 <hr>
 Agents Commission:  <select name="commission" id="commission">
 <option value="10%">10%</option>

 <option value="12%" selected="selected">12%</option>
 </select> <hr>
 Estimated Setup Fees:&pound; <input id="fees" name="fees" type="text" /><hr>

 How many weeks in the year do you estimate <br>that your property will<br> be    vacant/empty?<input name="weeks" type="text" /><hr>
 <button class="submit button2" name="submit" type="submit" value="calculate"><span   class="next">></span>calculate</button>
 </form>

 </div>

3 个答案:

答案 0 :(得分:0)

您的提交操作应在提交第一个表单时进行验证,然后显示其他表单。请尝试以下代码。

<script>
$(document).ready(function(){
     $("#form1").submit(function() {
     $("#form1").validate();
  /* AJAX calls and insertion into #productionForm */
    $("#form2").show();
    return false;
    });
});
</script>   

答案 1 :(得分:0)

尝试使用jQuery Form Plugin:http://malsup.com/jquery/form/

将其放在以下部分:

<script src="http://malsup.github.com/jquery.form.js"></script>

然后使用ajaxForm修改脚本:

<script>
$("#form1").ajaxForm(function() {
/* AJAX calls and insertion into #productionForm */
$("#form2").show();
return false;
});
</script> 

答案 2 :(得分:0)

以下是我在评论Harun Thuo时所说的话:

$(".selector").validate({
  rules: {
    // simple rule, converted to {required:true}
    name: "required",
    // compound rule
    email: {
      required: true,
      email: true
    }
  },
  submitHandler: function(form) {
    // do other things for a valid form
    form.submit();
    $("#form2").show();
      return false;
    });
  }
});

您可以在此处提交。阅读文档。您还可以控制错误放置。

http://jqueryvalidation.org/validate