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条)

报告相同问题?

悬赏问题

  • ¥30 微信小程序蓝牙数据透传
  • ¥15 加氢站氢负荷数据集来源
  • ¥15 umi接入sentry遇到问题
  • ¥15 HBuilderX打包H5网页,扫码模块无法使用
  • ¥15 Javascript跳转页面后,无法执行后面代码,如何解决?
  • ¥15 echarts绘制图表
  • ¥15 请教两个关于高德地图定位不准的技术问题
  • ¥15 根据企业名称 对照两个文件 样本筛选/匹配
  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据