Help onload in <body> to perform ajax apros

$("#kilometers").change(function(){ var searchString = $(this).val(); $.ajax({ url: "index_ajax.php", dataType : "html", data: "q=includes/modules/ajax/LoadCityTax.php&name="+searchString, type: "POST", success: function(msg){$("#city").html(msg);} }); }); 

I want to output the result of the query to <input id="kilometers">

  • "by onload in <body>" is it like? - Zowie
  • <body onLoad = "request function ()"> - KoVaLsKy
  • So what's the problem? 'I want to output the result of the query to <input id = "kilometers">'. function (msg) {$ ("# city"). html (msg)}} // in input with id kilometers say? :) - Zowie
  • $("#kilometers").change(function(){/*...*/}).change(); - that's all. Well, and wrap the function in $(function(){/* код */}); =) - ling

1 answer 1

Enough to add where you have a script with ajax request

 $(window).load(function(){ $('#kilometers').trigger('change'); }); 

or just inside $ (document) .ready ()

 $('#kilometers').trigger('change'); 
  • Ie such a design? <script type = "text / javascript"> $ (document) .ready (function () {$ ("# kilometers"). trigger ("change"); $ ("# kilometers"). change (function () { var searchString = $ (this) .val (); $ .ajax ({url: "index_ajax.php", dataType: "html", data: "q = includes / modules / ajax / LoadCityTax.php & name =" + searchString, type: "POST", success: function (msg) {$ ("# kilometers"). html (msg);}});});}); </ script> - KoVaLsKy
  • $ ("# kilometers"). trigger ("change"); - then put $ (document). ready (function () {$ ("# kilometers"). change (function () {var searchString = $ (this). val (); $ .ajax ({url: "/ echo / html / ", dataType:" html ", data:" q = includes / modules / ajax / LoadCityTax.php & name = "+ searchString, type:" POST ", success: function (msg) {$ (" # city ") .html (msg);}});}); $ ("# kilometers"). trigger ("change");}); - Deonis
  • PS By the way, @ling offered you the most concise solution. But posuti is the same. Just the trigger can be used in different cases and at different events - Deonis
  • Even I’m really going by the mind (((from <input type = "text" id = "city" value = "'. $ city." "" /> you need to get the value and after ajax processing, stick it into <input type = "text "id =" kilometers "name =" prices "/> '; and in the script I write function (msg) {$ (" # city "). html (msg); bda ((( - KoVaLsKy
  • The question was " ajax request for onload ", now we move somewhere off the side " we need to get the value and after ajax processing we shove it (I won't say where)" - Deonis