jQuery onLoad Textbox更新

时间:2015-12-01 16:38:40

标签: jquery

我有以下代码,我希望函数在加载时执行。

  $(document).ready(function() {
var runningRequest = false;
var request;

$('input#asd3').on('load input change propertychange onpaste', function(e) {

    var $q = $(this);
    if($q.val.length == 12){


        return false;
    }

    if(runningRequest){
        request.abort();
    }
    runningRequest=true;

    var myString = self.location.href;
    var mySplitResult = myString.split("?");

    request = $.getJSON('apisearch?t=usr',{q:$q.val()},function(data){  
        showResults2(data,$q.val());         
        showResultsrecruit(data,$q.val());
        showResultsaddress(data,$q.val());
        showResultseducation(data,$q.val());
        showResultshigheducation(data,$q.val());
        showResultsexperience(data,$q.val());
        showResultselections(data,$q.val());            
        runningRequest=false;

    });
    ...

PHP。

$mykad = ''.htmlentities($_GET['mykad']).'';

HTML:

echo '<input type="text" id="asd3" name="asd3" value="'.$mykad.'"  class="form-control" autocomplete="on" placeholder="Enter 12 digit MyKad..." class="input-block-level" autofocus="autofocus" placeholder="Search Mykad..." maxlength="12" style="width:90%;" />';

它获取文本框中mykad的值,然后假设执行搜索功能。

如果我更改文本框,点击焦点等,它可以正常工作。但它不会在页面加载时执行。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试:

$(document).ready(function() {
var runningRequest = false;
var request;
 var $q = $('input#asd3');
function toexecute($q) {
    request = $.getJSON('apisearch?t=usr',{q:$q.val()},function(data){  
        showResults2(data,$q.val());         
        showResultsrecruit(data,$q.val());
        showResultsaddress(data,$q.val());
        showResultseducation(data,$q.val());
        showResultshigheducation(data,$q.val());
        showResultsexperience(data,$q.val());
        showResultselections(data,$q.val());            
        runningRequest=false;

    });
}
 toexecute($q);
$('input#asd3').on('load input change propertychange onpaste', function(e) {

    var $q = $(this);
   if($q.val.length == 12){


        return false;
    }

    if(runningRequest){
        request.abort();
    }
    runningRequest=true;

    var myString = self.location.href;
    var mySplitResult = myString.split("?");
     toexecute($q)
    });
    ...