简体   繁体   English

在jQuery中从姓氏中转义单引号

[英]Escape single quote from last name in jQuery

I have ajax call in my jQuery code with which I am doing lookup with C# web method.我在我的 jQuery 代码中有 ajax 调用,我正在使用 C# web 方法进行查找。

The call works normally most of the time, the user needs to enter last name and it will get all the results for that last name.该呼叫在大多数情况下正常工作,用户需要输入姓氏,它将获得该姓氏的所有结果。

The problem happens when user will enter last name with single quote included such as O'leary .当用户输入包含单引号的姓氏时会出现问题,例如O'leary

This is the jQuery line I am using:这是我正在使用的 jQuery 行:

   data: "{selectedAgent: '" + $('#<%=txtAgentNameText.ClientID %>').val() + "', 
   companyID: '" + $('#<%=ddlCompany.ClientID %>').val() + "'}",

The problem happens with the txtAgentNameText.ClientID value.问题发生在txtAgentNameText.ClientID值上。

It looks like you are passing a JSON string to your jQuery ajax method.看起来您正在将 JSON 字符串传递给您的 jQuery ajax 方法。 Why not just pass a javascript object?为什么不只传递一个 javascript 对象?

var data = {
            selectedAgent:$('#<%=txtAgentNameText.ClientID %>').val(),
            companyID: $('#<%=ddlCompany.ClientID %>').val()
           };

$.ajax({...data:data...})

Okay so the escape character is \ so you could replace \' or you could change your format so the last name is contained in double quotes!好的,所以转义字符是 \ 所以你可以替换 \' 或者你可以改变你的格式,所以姓氏包含在双引号中! Both ways should suffice两种方式应该足够了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM