2

我是第一次根据此处的说明使用 jQuery 验证插件,但它不起作用。当我尝试提交必填字段为空的表单时,我看到错误消息,但表单不会提交。我是 jQuery 新手,所以我自己解决问题非常具有挑战性。

这不是由于这里建议的自动关闭脚本标签, 我也发现了这个,但我在控制台中没有看到任何错误。

我究竟做错了什么?

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

另一个也是 - 我正在使用 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>
4

1 回答 1

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>

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

于 2013-05-02T18:45:02.557 回答