如何在Javascript中ESCAPE“逗号”

时间:2011-02-14 21:44:51

标签: javascript asp.net

EDIT1:

btnDelete.Attributes.Add("onclick", String.Format(@"return DeleteRow('{0}',{1},{2},{3});", e.Row.ClientID, e.Row.RowIndex, DataBinder.Eval(e.Row.DataItem, "Id"), "'" + DataBinder.Eval(e.Row.DataItem, "Name") + "'"));

编辑:

我收到此错误:

消息:未终结字符串常量

我正在传递代码背后的值,而我的一些文字还有一些东西:

Foo3, In.c

   //javascript
    function DeleteRow(rowId, rowIdx, Id, Name) {         
        var textForMessage = "return confirm('Are you sure you want to delete this record with the name: \n{0} \n{1}');";
        //removed code...  
       return result;
    }

5 个答案:

答案 0 :(得分:10)

什么都不做。逗号在JavaScript字符串中没有特殊含义。

答案 1 :(得分:2)

您无需转义逗号。您应该用引号括起整个字符串

'Foo3, In.c'

如果此字符串位于另一个也是单引号的字符串中,则可能需要转义引号。

答案 2 :(得分:0)

如果错误是客户端,您可以通过以下方式解决:

btnDelete.Attributes["onclick"] = String.Format("return DeleteRow('{0}', '{1}', '{2}', '{3}');", e.Row.ClientID, e.Row.RowIndex, DataBinder.Eval(e.Row.DataItem, "Id"), DataBinder.Eval(e.Row.DataItem, "Name").ToString().Replace("'", "\'"));

如果是服务器端,请发布完整的错误消息和堆栈跟踪。

答案 3 :(得分:0)

\x2c

在ajax中很有用。

function myfunction(id, str_URL) {
     $.ajax({
         type: "GET",
         url: str_URL,
         success: function(t) {
             var link = "<a href=\x22JavaScript:MyJava(\x22Param1\x22\x2c\x22Param2\x22)\x22>Link text</a>";
         }

     });
}

\x22"(引用)

只需引用ascii表中的Hex列。

http://www.asciitable.com/index/asciifull.gif

答案 4 :(得分:0)

如果您试图避免因逗号引起的抛出错误,那么在非引号中可能是字符串数据类型。 说您需要post.example,+ post.example,

  var comma =",";

post.example.concat(comma) + post.example.concat(comma)

使用concat方法将连接任何变量类型。