2

我正在尝试将 jQuery datepicker 用于使用 jQuery 1.3.2 的网站上的表单,但它不起作用。对于我的一些表单功能,我必须引用一个较新的 jQuery 库,还必须引用 jQuery ui 才能使 datepicker 工作。我已将 noConflict 用于较新的 jquery 库,但它仍然无法正常工作。我得到 Uncaught TypeError: Cannot read property 'document' of null 在控制台中。更新/删除 1.3.2 参考不是一种选择。

这是我的小提琴,它有效。但我在 Chrome(不是 FF)中收到上述错误,并且 datepicker 无法在我的网站上运行。http://jsfiddle.net/pnA33/

任何人都可以帮忙吗?它在本地工作,但不在服务器上工作(这是一个开发环境,所以我不能分享链接)。我发现了这一点,但由于我对 jQuery 比较陌生,所以这超出了我的想象。

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>
            <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
            <script type="text/javascript">
            var jQuery_1_9_1 = jQuery.noConflict(true);
            jQuery_1_9_1(document).ready(function() {
            jQuery_1_9_1( "#datepicker" ).datepicker({ minDate: -1, maxDate: "+24D" });
            jQuery_1_9_1('#pancettaForm').change(function () {
                       jQuery_1_9_1('.address,#new-ship-date').hide();
                       if (jQuery_1_9_1('#ship-address').prop('checked')) {
                          jQuery_1_9_1('.address').show();
                       }
                       else if (jQuery_1_9_1('#ship-date').prop('checked')) {
                          jQuery_1_9_1('#new-ship-date').show();
                       }
                       else if (jQuery_1_9_1('#ship-both').prop('checked')) {
                          jQuery_1_9_1('.address, #new-ship-date').show();
                       }
                    });

            });

            function validateForm()
            {
            var x=document.forms["pancettaForm"]["order-number"].value;
            if (x==null || x=="")
            {
            alert("Please provide your order number from the confirmation email sent immediately after placing your order.");
            return false;
            }

            }
            </script> 

HTML:

        <form name="pancettaForm" method="post" action="http://lizlantz.com/lcform.php" id="pancettaForm" onsubmit="return validateForm()">
                <input type="hidden" value="Pancetta Order Update" name="subject"> 
                <input type="hidden" value="cookware/partners_10151_-1_20002" name="redirect">
                <ol>
                    <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">
                    </li>             
                    <li>                
                    <label for="full-name"><em>*</em>Recipient Full Name:</label> 
                        <input type="text" name="full-name">
                    </li>   
                    <li class="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 class="address" 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 value="AL">Alabama</option>
                            <option value="AK">Alaska</option>
                        </select>
                        <label for="zip">
                            <em>*</em>Zip Code
                        </label> 
                        <input type="text" name="zip">
                    </li>
                    <li id="new-ship-date"  style="display: none;">
                        <em>*</em><label for="updated-ship-date">New Ship Date:</label>
                        <input type="text" id="datepicker" name="updated-ship-date" value="Update Your Ship Date" />
                    </li>            
                    <li>
                        <label for="phone">
                            <em>*</em>Phone (for delivery questions)
                        </label> 
                        <input type="text" name="phone">
                    </li>               
                </ol>
                       <input type="submit" id="button" name="submit"  value="Update Pancetta">

              </form>
4

1 回答 1

1

啊哈!您实际上不能一次在同一个文档实例上运行两个版本的 jQuery ......因此这里出现了问题。这个解决方案直接来自Splendìd Angst在这个线程中的回答我可以在同一页面上使用多个版本的 jQuery 吗?

基本上,您必须在进入文档“主循环”之前声明您的 noConflict 版本,我想您可以调用它。

在 document.ready 调用之外声明你的 noConflict 变量...使用标准的 $(document).ready() 语法,然后在 ready 闭包中使用(并且只使用)你的 noconflict 变量。那应该这样做。

我还没有测试过这个提醒你,但它是有道理的,它不会让你对你的代码进行太多调整来尝试它。

TIL 一些关于 jQuery 的新东西 :)

于 2013-05-01T20:41:53.960 回答