0

我的 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>
4

3 回答 3

0

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

<script>
$(document).ready(function(){
     $("#form1").submit(function() {
     $("#form1").validate();
  /* AJAX calls and insertion into #productionForm */
    $("#form2").show();
    return false;
    });
});
</script>   
于 2013-09-18T07:46:22.920 回答
0

尝试使用 jQuery 表单插件: 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> 
于 2013-09-18T07:52:20.477 回答
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

于 2013-09-18T08:04:27.957 回答