简体   繁体   English

页面加载中的jQuery不起作用

[英]Jquery on page load not working

I am doing a Get Request in PHP to get a certain value, I am then taking this value and inputting it into a textfield's value, I have that text-fields value then call a json request. 我正在用PHP执行Get Request以获取某个值,然后将其取值并将其输入到textfield的值中,我拥有该text-fields的值,然后调用json请求。

It works on input and change, but on page load. 它适用于输入和更改,但适用于页面加载。 Here is the code below: 这是下面的代码:

$(document).ready(function() {
    var runningRequest = false;
    var request;
    $('input#asd3').on('load', function(e) {
        var $q = $(this);
         if(runningRequest){
             request.abort();
         }
         runningRequest=true;
         var myString = self.location.href;
         var mySplitResult = myString.split("?");
         request = $.getJSON('search',{q:$q.val()},function(data){  
             showResults(data,$q.val());
             showResults2(data,$q.val());           
             runningRequest=false;
         });
     });
 });

The html HTML

   <input type="text" id="asd3" name="asd3" value="<?php echo $name ?>" class="form-control" autocomplete="off" placeholder="Search Name..." class="input-block-level" placeholder="Search..."  style="width:100%; display:none;"  />

https://developer.mozilla.org/en-US/docs/Web/Events/load https://developer.mozilla.org/zh-CN/docs/Web/Events/load

The load event is primarily related to elements that pertain to external resources. 加载事件主要与与外部资源有关的元素有关。 The window document, images, iframes, etc. An input element does not rely on anything like that and would not have a load event. 窗口文档,图像,iframe等。input元素不依赖于此类内容,并且不会发生load事件。

If you want the logic to happen on 'page' load then you should bind to the window or document. 如果您希望逻辑在“页面”加载时发生,则应绑定到窗口或文档。 Try changing... 尝试改变...

$('input#asd3').on('load', ...

to

$(document).on('load', ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM