2012-02-22 179 views
0

我是jquery和php的新手,我有兩個輸入字段,zip和city,城市將輸出一個基於用戶輸入的zip的值。 jQuery的腳本將調用一個網址:http://domain.com/city?zip.php=「拉鍊;使zip.php將返回將輸出到城市輸入字段jquery ajax獲取請求

我用ajax getXMLHTTP試圖回聲值有時它的工作原理,但有時不

請參見下面的下面的代碼片段:

<input type="text" id="zip_code" name="zip_code" /> 

<input type="text" id="city" name="city" /> 

<script type="text/javascript"> 

// Some Jquery code here for ajax get request to http://domain.com/city?zip.php 

</script> 
+0

如果你使用jquery只使用'$ .get()' 你必須向我們展示一些代碼,或者我們不能幫你。 – Rufinus 2012-02-22 12:18:45

+0

http://api.jquery.com/jQuery.ajax/ – Neysor 2012-02-22 12:21:49

回答

0

使用jQuery.get ,記錄在here。在成功處理程序中,使用data參數填充城市輸入。

樣品:

$.get('http://domain.com/city.php?zip='+$('#IdOfZipInput').val(), function (data){ 
    $('#IdOfCityInput').val(data); 
}); 
0

使用jQuery AJAX例如:

var zip = $('#zip').val(); 
$.get('http://domain.com/city.php?zip='+zip,function (data){ 
    $('#city').val(data); 
}); 
4

如果你是我們荷蘭國際集團的jQuery使用$阿賈克斯選項,而不是getXMLHTTP

function passzipvalue(zip) 
$.ajax({ 
       type: "GET", 
       url : 'http://domain.com/city.php=' 
       data:"zip="+zip, 
       success: function(msg){ 
        $("#formsData").html(msg); 

       } 
      }); 
} 

這樣的事情還是

$.get('http://domain.com/city.php?zip='+zip,function (msg){ 
    $('#formsData').html(msg); 
}); 
如果你想填充它在某些輸入字段

使用.val代替.html

+0

#formsData是輸入字段嗎?如果是,* only *'.val()'工作;否則*只有*'.html()'或'.text()'起作用!除此之外,當從另一個域檢索數據時通常需要jsonp – ThiefMaster 2012-02-22 12:22:05

+0

我假定它是div,但是,是的 你說的也是正確的 – Poonam 2012-02-22 12:22:41

+0

在你的第二個代碼塊中,你使用'.val()'... – ThiefMaster 2012-02-22 12:22:58

0

嘗試使用jQuery的AJAX

$.ajax({ 
     type: "POST", 
     url: 'sample/test.php',//your url 
     data: data,//data to be post 
     cache: false, 
     success: function(html) { 
     alert(html);//response from the server 
    } 

     }); 
0
$.ajax({ 
    url: 'http://domain.com/city.php?zip='+zip, 
    type: get, 
    success: function(data){ 
    $("div").html(data); 
    } 
}); 

use this data will be displayed 
0

如果它是一個不斷更新的元素,然後使用jquery.post作爲緩存,即 「獲取」 的結果。

jQuery.post( 'call.php',{行動: 「得到」},功能(數據){

 jQuery('#content').append(data); 

    }); 

發現這裏的教程http://vavumi.com/?p=257