2014-10-04 26 views
-2

我爲我的網站使用PHP,jQuery,Smarty等。爲什麼我無法在以下情況下在項目的某個頁面中運行jQuery代碼?

我有一個窗體上有三個輸入文本字段, zip_code,城市和州。當用戶在輸入文本字段中輸入有效的美國郵政編碼時,我編寫了jQuery代碼以自動填充城市和州輸入文本字段zip_code

表單上的其他jQuery功能工作正常,並且完美,但是我只有使用此功能纔有問題。我爲此功能創建了一個演示頁面,此zip_code功能正常工作。但它在我的項目中不起作用。

我把首頁的整個HTML包含以下表格:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>Project Name</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="description" content="Example of Fixed Layout with Twitter Bootstrap version 2.0 from w3resource.com"> 
    <meta name="author" content=""> 

    <link href="http://localhost/project_folder/user_ui_files/css/bootstrap.css" rel="stylesheet">  
    <link href="http://localhost/project_folder/user_ui_files/css/example-fixed-layout.css" rel="stylesheet">  
    <link href="http://localhost/project_folder/user_ui_files/css/bootstrap-responsive.css" rel="stylesheet"> 
    <link href="http://localhost/project_folder/user_ui_files/css/bootstrap-modal.css" rel="stylesheet"> 
    <link href="http://localhost/project_folder/user_ui_files/css/slippry.css" rel="stylesheet"> 
    <link href="http://localhost/project_folder/user_ui_files/css/signin.css" rel="stylesheet" type="text/css"> 
    <link href="http://localhost/project_folder/user_ui_files/css/jquery.dateLists.css" rel="stylesheet" type="text/css"> 

    <style type="text/css"> 

    .hideme { 
     opacity:0; 
    } 
    @media (max-width: 979px) { 
     .navbar-fixed-top.navbar-absolute { 
     position: absolute; 
     margin: 0; 
     } 
    } 
    .navbar-absolute + div { 
     margin-top: 58px; 
    } 
    #footer { 
     position: absolute; 
     bottom: 0; 
     width: 100%; 
     /* Set the fixed height of the footer here */ 
     background-color: #f5f5f5; 
    } 
    </style> 
    </head> 
    <body> 
    <div class="navbar navbar-fixed-top navbar-absolute"> 
     <div class="navbar-inner"> 
     <div class="container"> 
      <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      <span class="icon-bar"></span> 
      </a> 
      <a class="brand" href="http://localhost/project_folder/index.php"><img src="http://localhost/project_folder/user_ui_files/img/logo.png"/></a> 
      <div class="nav-collapse"> 
      <ul class="nav pull-right navbar-fixed-bottom"> 
       <li><a href="http://localhost/project_folder/login.php"><i class="icon-user icon-black"></i> LOGIN</a></li> 
       <li><a href="http://localhost/project_folder/register.php"><i class="icon-pencil icon-black"></i> REGISTER</a></li> 
       <li><a href="http://localhost/project_folder/chk_rebate_status.php"><i class="icon-edit icon-black"></i> REBATE STATUS</a></li> 
       <li><a href="http://localhost/project_folder/contact_us.php"><i class="icon-envelope icon-black"></i> CONTACT</a></li> 
        <li><a href="storelocations.php"><i class="icon-map-marker icon-black"></i> STORE LOCATOR</a></li> 
       <li> 
       <form action="index.php" class="navbar-form pull-right" id="formzip" method="post"> 
        <input type="hidden" class="form-control" name="op" id="op" value="zip_code"> 
        <input type="hidden" class="form-control" name="form_submitted" id="form_submitted" value="yes"> 
        <input style="width: 115px;" type="text" placeholder="Enter the zip code" name="zip_code" id="zip_code" value="" > <i class="icon-zip" style="margin-top:3px;" onclick='$("#formzip").submit();'></i> 
       </form> 
       </li>     
      </ul> 
      </div><!--/.nav-collapse --> 
     </div> 
     </div> 
    </div> 
    <style type="text/css"> 
    .list { 
     width:60px; 
    } 

    .dateLists_container { 
    } 

    .dateLists_container .list { 
     float:left; 
    } 

    .dateLists_container .day_container { 
    } 

    .dateLists_container .day_container .list { 
     margin-right:10px; 
    } 

    .dateLists_container .month_container { 
    } 

    .dateLists_container .month_container .list { 
     margin-right:10px;  
    } 

    .dateLists_container .year_container { 
    } 

    .dateLists_container .year_container .list { 
    } 
    </style> 
    <div class="container" style="padding-top: 140px; margin-bottom: 90px;"> 
     <div class="row"> 
     <div class="span12 account-container12">   
      <legend>New User? Register</legend> 
      <form action="register.php" class="form-horizontal" method="post"> 
      <div class="row"> 
       <input type="hidden" class="form-control" name="op" id="op" value="preview"> 
       <input type="hidden" class="form-control" name="form_submitted" id="form_submitted" value="yes"> 
       <input type="hidden" class="form-control" name="main_op" id="main_op" value=""> 
       <div class="col-xs-1"></div>      
      </div> 
      <div class="span6"> 
       <div style="float: clear;"></div> 
       <fieldset> 
       <!-- Form Name --> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="First Name">First Name<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="first_name" id="first_name" value="" type="text" placeholder="Enter your first name"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Last Name">Last Name<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="last_name" id="last_name" value="" type="text" placeholder="Enter your last name">  
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Street 1">Address 1<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="street1" id="street1" value="" type="text" placeholder="Enter the address"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Street 2">Address 2</label> 
        <div class="controls"> 
        <input name="street2" id="street2" value="" type="text" placeholder="Enter your address"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Zip">Zip<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="zip_code" id="zip_code" value="" type="text" placeholder="Enter your zip code" class="input-medium"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="City">City<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="city" id="city" value="" readonly="readonly" type="text" placeholder="Select your city" class="input-medium"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="State Code">State<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="state_code" id="state_code" value="" readonly="readonly" type="text" placeholder="Enter state code" class="input-medium"> 
        </div> 
       </div> 
       </fieldset>  
      </div> 
      <div class="span5"> 
       <fieldset> 
       <!-- Text input-->    
       <div class="control-group"> 
        <label class="control-label" for="DOB">Date Of Birth<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input class="form-control date_control" type="text" name="dob" id="dob" value=""> 
        </div> 
       </div>    
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Email Id">Email Id<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="email" id="email" value="" type="text" placeholder="Enter your mail ID"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="num">Phone No.</label> 
        <div class="controls"> 
        <input name="phone_no" id="phone_no" value="" type="text" placeholder="Enter phone no." class="input-medium"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Password">Password<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="password" id="password" value="" type="password" placeholder="Enter the Password" class="input-medium"> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div class="control-group"> 
        <label class="control-label" for="Password">Confirm Password<span style="color:#FF0000">*</span></label> 
        <div class="controls"> 
        <input name="password_confirmation" id="password_confirmation" value="" type="password" placeholder="Re-enter the password" class="input-medium"> 
        </div> 
       </div> 
       <!-- Multiple Radios --> 
       <div class="control-group"> 
        <label class="control-label" for="radios">Mode of Payment</label> 
        <div class="controls"> 
        <label class="radio" for="radios-0"> 
        <input type="radio" name="mode_of_payment" value="paypal"> 
        PayPal 
        </label> 
        <label class="radio" for="radios-1"> 
        <input type="radio" name="mode_of_payment" value="check"> 
        Check 
        </label> 
        </div> 
       </div> 
       <!-- Text input--> 
       <div id="paypal_op" style="display:none;" class="control-group"> 
        <label class="control-label" for="email">PayPal Email Account</label> 
        <div class="controls"> 
        <input type="text" name="pay_pal_email" id="pay_pal_email" value="" placeholder="Enter email id" class="input-large"> 
        </div> 
       </div> 
       </fieldset>   
      </div> 
      <div class="container"> 
       <div class="row"> 
       <div class="span2"></div> 
       <div class="span5"> 
        <button type="submit" class="btn btn-success"><i class="icon-white icon-ok"></i> Preview</a> 
        </div> 
       </div> 
      </div> 
      </form>   
     </div> 
     </div> 
    </div> <!-- /container -->  
    <footer style="background-color:#000" id="footer"> 
     <div class="container"> 
      <div class="row"> 
      <div class="span3 top-buffer-footer"> 
      <p> &copy; 2014 PROJECT NAME</p> 
      </div> 
      <div align="center" class="span7 top-buffer-footer"> 
      <a href="#">About Us</a> | 
       <a href="#">Privacy Policy</a> | 
       <a href="#">Terms & Conditions</a> 
      </div> 
      <div class="span2 top-buffer-footer"> 
      <a style="float:right" href="#">Powered By COMPANY NAME</a> 
      </div> 
     </div> 
     </div>  
    </footer>  

    <!-- Le javascript 
    ================================================== --> 
    <!-- Placed at the end of the document so the pages load faster and more quicker--> 
    <script src="http://localhost/project_folder/user_ui_files/js/jquery.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/slippry.min.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-tooltip.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-alert.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-button.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-carousel.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-collapse.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-dropdown.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-modal.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-popover.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-scrollspy.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-tab.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-transition.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-typeahead.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/bootstrap-modalmanager.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/jquery.dateLists.min.js"></script> 
    <script src="http://localhost/project_folder/user_ui_files/js/custom/common.js"></script>  
    </body> 
</html> 

jQuery的代碼來運行這個郵政編碼功能寫入文件common.js其中包括最後本頁面。

以下是代碼文件common.js

$(document).ready(function() { 
/* Every time the window is scrolled ... */ 
    $(window).scroll(function(){ 
    /* Check the location of each desired element */ 
    $('.hideme').each(function(i) { 

     var bottom_of_object = $(this).position().top + $(this).outerHeight(); 
     var bottom_of_window = $(window).scrollTop() + $(window).height(); 

     /* If the object is completely visible in the window, fade it it */ 
     if(bottom_of_window > (bottom_of_object - (bottom_of_object * 0.2))) { 

     $(this).animate({'opacity':'1'},300); 

     } 

    }); 
    }); 
    /*jQuery code for autopo-populate city and state when customer enters valid zip code*/ 
    $("#zip_code").keyup(function() { 
    var el = $(this); 

    if (el.val().length === 5) { 
     $.ajax({ 
     url: "http://zip.elevenbasetwo.com", 
     cache: false, 
     dataType: "json", 
     type: "GET", 
     data: "zip=" + el.val(), 
     success: function(result, success) { 
      $("#city").val(result.city); 
      $("#state_code").val(result.state); 
     } 
     }); 
    } 
    }); 

    $('#dob').dateDropDowns({dateFormat:'mm-dd-yy'});  
    $("input[type='radio']").change(function() { 
    if($(this).val()=="paypal") { 
     $("#paypal_op").show(); 
    } else { 
     $("#paypal_op").hide(); 
    }  
    }); 
}); 

$(function() { 
    var demo1 = $("#demo1").slippry({ 
    transition: 'fade', 
    useCSS: true, 
    speed: 1000, 
    pause: $('#brand_slider_time').val() * 1000, 
    auto: true, 
    preload: 'visible' 
    }); 
    $('.stop').click(function() { 
    demo1.stopAuto(); 
    }); 
    $('.start').click(function() { 
    demo1.startAuto(); 
    }); 
    $('.prev').click(function() { 
    demo1.goToPrevSlide(); 
    return false; 
    }); 
    $('.next').click(function() { 
    demo1.goToNextSlide(); 
    return false; 
    }); 
    $('.reset').click(function() { 
    demo1.destroySlider(); 
    return false; 
    }); 
    $('.reload').click(function() { 
    demo1.reloadSlider(); 
    return false; 
    }); 
    $('.init').click(function() { 
    demo1 = $("#demo1").slippry(); 
    return false; 
    }); 
}); 

我使用jQuery V1.7.1在我的項目。

我也創建了一個jsFiddle只有這些必要的領域它工作正常。你可以看到小提琴here

在我的項目中,我嘗試調試代碼,試圖將警報放入我爲zip_code編寫的函數中。 Firebug控制檯都沒有顯示任何錯誤,也沒有打印警報。

然後我嘗試在打印的zip_code函數(即頁面加載)之外打印警報。警報沒有打印在函數內部。此外,我嘗試各種其他事件,如焦點,模糊等,而不是關鍵,然後警報也沒有打印。 Firebug控制檯從未向我顯示任何錯誤或警告。

+2

你應該專注於什麼*不工作*。你的小提琴只是演示*正在工作*。具體來說,請詳細說明這一點 - *它不適用於我的項目*。 – Boaz 2014-10-04 14:00:07

+0

@Boaz:我添加了關於我所嘗試的內容以及我在項目中遇到的問題的解釋。這很好,還是你想要更多的東西嗎? – PHPLover 2014-10-04 14:09:59

+0

現在更清晰了。您是否嘗試過使用Chrome等其他瀏覽器進行檢查?您是否徹底清除了緩存?確保您所做的更改實際上存在於正在提供給瀏覽器的腳本中。 – Boaz 2014-10-04 14:16:04

回答

1

您帖子中的標記包含兩個form元素,其中一個顯示爲導航菜單,另一個顯示爲該頁面的主要內容。這兩個表單都有一個輸入元素,其編號爲zip_code

在HTML id屬性意味着是獨特的。出於這個原因,表達式$('#zip_code')將僅返回具有該id的兩個元素中的第一個 - 導航菜單中的一個。這反過來導致keyup事件不被綁定到第二個(和相關的)輸入字段。

它在你的小提琴中起作用的原因是小提琴中的HTML不包括導航菜單,因此標記僅包含帶有zip_code id的單個輸入元素。

+0

:完美的男人,你救了我的命。檢測到確切的問題。非常感謝你的寶貴幫助。我已經接受了你的答案並且也贊成了。 – PHPLover 2014-10-04 15:21:42

相關問題