空格中的jquery结束字符串

Rya*_*yan 1 variables jquery whitespace

我有一个'创建标签'的表单.使用下面的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;
  });
Run Code Online (Sandbox Code Playgroud)

但我刚刚意识到,如果'newtag'变量包含一个空格,那么它将会结束.通过firebug观察它,如果没有空间参数显示如下:

company_id 5495
newtag test
type_id 2
Run Code Online (Sandbox Code Playgroud)

但是当输入空格时,它看起来像这样:

newtag test
Run Code Online (Sandbox Code Playgroud)

有人知道为什么会发生这种情况吗?为什么它没有将适当的变量传递给加载的页面?

提前致谢!

瑞安

cle*_*tus 11

使用encodeURIComponent()值:

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