doty58493 2012-09-14 15:19
浏览 110
已采纳

传递具有特殊字符的字符串

I am trying to pass strings with spaces and special characters, but its getting error and nothing is working. e.g.

 <img onclick='addKeyword("celebritynews&gossip");' src="images/plus.png" >

but the '&' inside string celebritynews&gossip is breaking the code.

I figured out that the problem is in addKeyword function, I'd used the following code for defining a variable named url,

function addKeyword(categoryId){
var url = "addKeyword.php?uid=" + user_id + "&categoryId=" + categoryId + "&keyword=" + $("#keyword_" + categoryId).attr("value");
}

and that $("#keyword_" + categoryId) is causing problem. Any way to solve that?

  • 写回答

3条回答 默认 最新

  • douerqu2319 2012-09-14 15:21
    关注

    Assuming you are passing the string on a URL, the parameter values need to be URL encoded.

    ...SUMMARY EDIT...

    The & is interpreted as HTML and must be HTML encoded, specifically &amp;

    HTML

    <img onclick='addKeyword("celebritynews&amp;gossip");' src="images/plus.png" />
    

    JavaScript

    Use encodeURIComponent(s).

    var url = "addKeyword.php?uid=" + encodeURIComponent(user_id) + "&categoryId=" + encodeURIComponent(categoryId) + "&keyword=" + encodeURIComponent($("#keyword_" + categoryId).attr("value")); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题