jquery mobile : Uncaught TypeError: Cannot read property 'abort' of undefined error in ajax call
你好,我已经用jquery移动设备制作了一个网站,我正在使用自动完成的jquery移动设备我想要中止以前的ajax请求,但我面临
这是我的代码:
1 2 | <ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Find a city..." data-filter-theme="a"> </ul> |
这里是js代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <script type="text/javascript"> var xhr ; $(document).on("pageinit", function() { autoComplete(); }); function autoComplete(){ $(".ui-input-text" ).on("keyup", function ( e, data ) { var $ul = $('#autocomplete'), $input = $(this), value = $input.val(), html =""; $ul.html("" ); if ( value && value.length > 1 ) { $ul.html(" <li> <span class='ui-icon ui-icon-loading'></span> </li> " ); $ul.listview("refresh" ); console.log(xhr); if(xhr && xhr.readystate != 4 && typeof xhr !== 'undefined'){ xhr.abort(); } xhr = $.ajax({ url:"http://gd.geobytes.com/AutoCompleteCity", type:'POST', dataType:"jsonp", data: { q: $input.val(), } }) .then( function ( response ) { $ul.html( response ); $ul.listview("refresh" ); $ul.trigger("updatelayout"); }); } }); } |
这是
需要对你的功能进行一些修改,用这个替换你的功能: -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | function autoComplete(){ $(".ui-input-text" ).on("keyup", function ( e, data ) { var $ul = $('#autocomplete'), $input = $(this), value = $input.val(), html =""; $ul.html("" ); if ( value && value.length > 1 ) { $ul.html(" <li> <span class='ui-icon ui-icon-loading'>/span> </li> " ); $ul.listview("refresh" ); if(xhr && xhr.readystate != 4){ xhr.abort(); } xhr = $.ajax({ url: site_url+"jewels/GetLeftPanel", type:'POST', data: { Type: $('#btn_jtype').val(), Group: $('#btn_gid').val(), tagValue: $('#btn_tag_select').val(), Searching: $input.val(), }, success: function(response){ $ul.html( response ); $ul.listview("refresh" ); $ul.trigger("updatelayout"); } }); } }); } |