jquery ending string at space
I have a form to 'create a tag'. Using the jQuery code below.
$("#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;
});
But i have just realised, if the 'new开发者_JAVA百科tag' variable includes a space, thats where it will end. Watching it through firebug, if theres no space the parameters appear like this:
company_id 5495
newtag test
type_id 2
But when a space is entered, it appears like this:
newtag test
Does anybody know why this could be happening? Why it is not passing the proper variables to the loaded page?
Thanks in advance!
Ryan
Use encodeURIComponent()
on the values:
$('#createtag').load("../contacts/action_createtag.php?newtag="+
encodeURIComponent(newtag) + "&type_id=" + encodeURIComponent(type_id) +
"&company_id=" + encodeURIComponent(company_id)).append('#createtags');
You need to encode your variables.
Use encodeURIComponent()
I think you must encode your space as a %20
in the URL because if you pass it as a space it will mark the end of the URL. I don't think that jQuery load does any kind of special character escaping by itself.
Encode your newtag value with encodeURI()
$('#createtag').load("../contacts/action_createtag.php?newtag="+ encodeURI(newtag) + "&type_id=" + encodeURI(type_id) + "&company_id=" + encodeURI(company_id)).append('#createtags');
精彩评论