2014-09-29 47 views
-1

我在使用jquery中的負載發送數據時遇到了問題。下面的例子。用jquery load發送數據url

<script> 
function load_locations (tipo_send,value_sel) { 
    jQuery("#location").load("modulos/mod_register/index_register.php?send_location=ok&tipo_send="+tipo_send+"&data_loc="+value_sel+"&city_post="+city_post); 
} 
</script> 
<?php 
print "<select name='reg[pais]' id='sel_1_country' class='register_input_text' onchange=\"load_locations('region',this.value);\">"; 
print "<option value='reinicio' onclick=\"load_locations('reinicio',this.value);\">Seleccionar País</option>"; 
print "<option>London City</option></select>"; 
?> 

該腳本工作正常。當我使用「倫敦城市」的空間時,問題就出現了。當我使用沒有空格的「London-City」時,問題不會出現。

我試過urlencode等,但它沒有奏效。

+0

''我嘗試所有,urlencode「' - 我懷疑事實並非如此,因爲URL編碼的值正是如何處理查詢字符串參數中的空格。 – David 2014-09-29 16:23:57

+0

是的,當我使用由空格分隔的名稱時,總是會出現問題,在這一刻腳本沒有工作,但是如果我放了一些「 - 」「_」等等,永遠不會失敗 – Francisco 2014-09-29 16:28:18

+0

對,查詢字符串值中的空格需要被網址編碼。這就是你解決這個問題的方法。如果您的*鍵*中的空格對於您的值有問題,則可以通過不使用鍵中的空格來解決*問題。這究竟是如何不工作? – David 2014-09-29 16:30:01

回答

0

使用encodeURIComponent編碼數據值:

jQuery("#location").load("modulos/mod_register/index_register.php?send_location=ok&tipo_send="+encodeURIComponent(tipo_send)+"&data_loc="+encodeURIComponent(value_sel)+"&city_post="+encodeURIComponent(city_post)); 

當使用jQuery.load()一個space用於在文檔中urlfragment(例如,元素ID)分開,你想,而不是加載來加載整個頁面。