jQuery Validation PlugIn问题 - 表单不会提交

时间:2013-05-02 18:28:42

标签: jquery forms jquery-validate

我第一次使用jQuery验证插件based on the instructions here并且它无效。当我尝试将表单与必填字段提交为空时,我看到错误消息,但表单将不会提交。我是jQuery的新手,所以这对我自己进行故障排除非常具有挑战性。

这不是由于建议here自行关闭脚本标记 我也发现了this,但我没有在控制台中看到任何错误。

我做错了什么?

此外,在我在必填字段中输入内容后,消息将消失,并且类“有效”将与错误类一起添加到输入中。这是不可取的 - 我该如何删除它?

另一个 - 我在使用1.3.2库(我无法更改或删除)的网站上实现此功能,并且字段上的显示/隐藏将无法使用,因此我必须运行一个新的并行库。

我在这里贴了一个小提琴:http://jsfiddle.net/4mhNC/1/

这是我的代码:

脚本:

            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>       
            <script src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js" type="text/javascript"></script>
            <script type="text/javascript">
            jQuery.validator.setDefaults({
                debug: true,
                success: "valid"
            });
            </script>                
            <script>var $j = jQuery.noConflict(true);</script>                
            <script type="text/javascript">
            $j(document).ready(function() {
                $j('#pancettaForm').change(function () {
                           $j('#address,#new-ship-date').hide();
                           if ($j('#ship-address').prop('checked')) {
                              $j('#address, #address2').show();
                           }
                           else if ($j('#ship-date').prop('checked')) {
                              $j('#new-ship-date').show();
                           }
                           else if ($j('#ship-both').prop('checked')) {
                              $j('#address, #address2, #new-ship-date').show();
                           }
                        });
                        $j("#pancettaForm").validate();

            });


            </script> 

HTML:

            <form name="pancettaForm" method="post" action="http://lizlantz.com/lcform.php" id="pancettaForm">
                    <input type="hidden" value="Pancetta Order Update" name="subject"> 
                    <input type="hidden" value="cookware/partners_10151_-1_20002" name="redirect">
                    <ul>
                        <li>
                        <label for="update-ship">I'd like to:</label> 
                            <input id="ship-address" name="update-ship" type="radio" value="update-ship-address"/> Have pancetta shipped to a different address than my skillet<br />
                            <input id="ship-date" name="update-ship" type="radio" value="update-ship-date" /> Have pancetta shipped sooner than June 14, 2013 <br />
                            <input id="ship-both" name="update-ship" type="radio" value="update-both" /> Make changes to both the shipping address and shipping date
                        </li>
                        <li>                
                        <label for="order-number"><em>*</em>Order Number (available in order confirmation email):</label> 
                            <input type="text" name="order-number" class="required">
                        </li>             
                        <li>                
                        <label for="full-name"><em>*</em>Recipient Full Name:</label> 
                            <input type="text" name="full-name">
                        </li>   
                        <li id="address" style="display: none;">
                            <label for="address">
                                <em>*</em>Address
                            </label> 
                            <input type="text" name="address">
                            <label for="address2">
                                Address Line 2
                            </label> 
                            <input type="text" name="address2">
                        </li>
                        <li id="address2" style="display: none;">
                            <label for="city">
                                <em>*</em>City
                            </label> 
                            <input type="text" name="city">
                            <label for="state">
                                <em>*</em>State
                            </label> 
                            <select name="state">
                                <option>- Select State -</option>                            
                                <option value="AL">Alabama</option>
                                <option value="AK">Alaska</option>
                                <option value="AZ">Arizona</option>
                                <option value="AR">Arkansas</option>
                                <option value="CA">California</option>
                                <option value="CO">Colorado</option>
                                <option value="CT">Connecticut</option>
                                <option value="DE">Delaware</option>
                                <option value="DC">District of Columbia</option>
                                <option value="FL">Florida</option>
                                <option value="GA">Georgia</option>
                                <option value="HI">Hawaii</option>
                                <option value="ID">Idaho</option>
                                <option value="IL">Illinois</option>
                                <option value="IN">Indiana</option>
                                <option value="IA">Iowa</option>
                                <option value="KS">Kansas</option>
                                <option value="KY">Kentucky</option>
                                <option value="LA">Louisiana</option>
                                <option value="ME">Maine</option>
                                <option value="MD">Maryland</option>
                                <option value="MA">Massachusetts</option>
                                <option value="MI">Michigan</option>
                                <option value="MN">Minnesota</option>
                                <option value="MS">Mississippi</option>
                                <option value="MO">Missouri</option>
                                <option value="MT">Montana</option>
                                <option value="NE">Nebraska</option>
                                <option value="NV">Nevada</option>
                                <option value="NH">New Hampshire</option>
                                <option value="NJ">New Jersey</option>
                                <option value="NM">New Mexico</option>
                                <option value="NY">New York</option>
                                <option value="NC">North Carolina</option>
                                <option value="ND">North Dakota</option>
                                <option value="OH">Ohio</option>
                                <option value="OK">Oklahoma</option>
                                <option value="OR">Oregon</option>
                                <option value="PA">Pennsylvania</option>
                                <option value="RI">Rhode Island</option>
                                <option value="SC">South Carolina</option>
                                <option value="SD">South Dakota</option>
                                <option value="TN">Tennessee</option>
                                <option value="TX">Texas</option>
                                <option value="UT">Utah</option>
                                <option value="VT">Vermont</option>
                                <option value="VA">Virginia</option>
                                <option value="WA">Washington</option>
                                <option value="WV">West Virginia</option>
                                <option value="WI">Wisconsin</option>
                                <option value="WY">Wyoming</option>
                            </select>
                            <label for="zip">
                                <em>*</em>Zip Code
                            </label> 
                            <input type="text" name="zip">
                        </li>
                        <li id="new-ship-date" style="display: none;">
                            <label for="New Ship Date"><em>*</em>New Ship Date:</label>                 
                            <select name="newShip" id="newShip">
                                <option>- Select -</option>
                                <option value="Wednesday, May 22">Wednesday, May 22</option>
                                <option value="Thursday, May 23">Thursday, May 23</option>
                                <option value="Friday, May 24">Friday, May 24</option>
                                <option value="Wednesday, May 29">Wednesday, May 29</option>
                                <option value="Thursday, May 30">Thursday, May 30</option>
                                <option value="Friday, May 31">Friday, May 31</option>
                                <option value="Wednesday, June 5">Wednesday, June 5</option>
                                <option value="Thursday, June 6">Thursday, June 6</option>
                                <option value="Friday, June 8">Friday, June 8</option>
                                <option value="Wednesday, June 12">Wednesday, June 12</option>
                                <option value="Thursday, June 13">Thursday, June 13</option>
                                <option value="Friday, June 14">Friday, June 14 </option>
                                <option value="Wednesday, June 19">Wednesday, June 19</option>
                                <option value="Thursday, June 20">Thursday, June 20</option>
                                <option value="Friday, June 21">Friday, June 21</option>
                            </select>                            
                        </li>                       
                        <li>
                            <label for="phone">
                                <em>*</em>Phone (for delivery questions)
                            </label> 
                            <input type="text" name="phone">
                        </li>               
                    </ul>
                           <input type="submit" id="button" name="submit"  value="Update Pancetta">

                  </form>

1 个答案:

答案 0 :(得分:3)

设计debug: true选项会阻止表单提交。

删除它。


由于某种原因,你还包含了两个版本的jQuery。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

这是一种不好的做法,因为它们可能互相干扰,所以删除旧版本。