2009-10-16 115 views
1

我有一個'創建標籤'的表單。使用下面的jQuery代碼。jquery在空格處結束字符串

$("#createtag").submit(function() { //same as above, but for form submit instead of button click 
    var newtag  = $('#newtag').attr('value'); 
    var type_id  = $('#type_id').attr('value'); 
    var company_id  = $('#company_id').attr('value'); 
     $('#createtag').load("../contacts/action_createtag.php?newtag="+ newtag + "&type_id=" + type_id + "&company_id=" + company_id).append('#createtags'); 
    return false; 
    }); 

不過我已經意識到,如果「newtag」變量包含空格,那它就會結束。看着它通過螢火蟲,如果世界上沒有空間的參數顯示如下:

company_id 5495 
newtag test 
type_id 2 

不過是進入了一個空當,這似乎是這樣的:

newtag test 

有誰知道這可能是爲什麼發生?爲什麼它沒有將正確的變量傳遞給加載的頁面?

在此先感謝!

瑞安

回答

11

上的值,用encodeURIComponent()

$('#createtag').load("../contacts/action_createtag.php?newtag="+ 
    encodeURIComponent(newtag) + "&type_id=" + encodeURIComponent(type_id) + 
    "&company_id=" + encodeURIComponent(company_id)).append('#createtags'); 
+0

爲我節省了一百萬小時.. – Fergus 2014-12-05 05:54:22

1

我想你一定編碼您的URL中的%20的空間,因爲如果你把它當作一個空間,它將標誌着該網址的結尾。我不認爲jQuery加載本身會有任何特殊字符轉義。

+0

的空間在這個例子中的問題,但如果有人把一個什麼符號在現場? – nickf 2009-10-16 05:55:35

1

與是encodeURI編碼您newtag值()

$('#createtag').load("../contacts/action_createtag.php?newtag="+ encodeURI(newtag) + "&type_id=" + encodeURI(type_id) + "&company_id=" + encodeURI(company_id)).append('#createtags');