jQuery 使用单引号将参数传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19399610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a parameter to function with single quote
提问by Billa
How do I pass a parameter to a javascript function with '
included
如何将参数传递给'
包含的 javascript 函数
var name ="Lauren O'Donald";
var htmlAnch='<a onclick="javascript:selectEmployee(1100,"'+name+'");return false;"
href="javascript:void(0);">O'Donald, Lauren</a>';
$(document).append($(htmlAnch));
The javascript function is not executing since the name 'Lauren O'Donald'
contains single quote.
javascript 函数未执行,因为名称'Lauren O'Donald'
包含单引号。
How can I add a parameter with '
and prepare dynamic html to make it work?
如何添加参数'
并准备动态 html 以使其工作?
Here is the dynamic code to generate
这是要生成的动态代码
var rows = new StringBuffer();
$(data).each(function(index) {
rows.append(String.format('<tr><td><a href="No.aspx"
onclick="javascript:selectEmployee({3},\"{1} {2}\");return
false;">{0}</a></td></tr>',
String.format("{0}, {1}", this.Surname, this.FirstName),
this.Surname,
this.FirstName,
this.Id
));
});
回答by h2ooooooo
You can escape quotes/characters by prepending \
to it:
您可以通过添加引号来转义引号/字符\
:
var string = 'my string with "double quotes" and \'single quotes\'';
var string = "my string with 'single quotes' and \"double quotes\"";
// ^ ^
Using a dynamic string:
使用动态字符串:
var foo = "bar with 'quotes'";
var string = 'my string with "double quotes" and ' + foo.replace(/'/g, "\'");
//my string with "double quotes" and bar with \'quotes\'
回答by Rory McCrossan
You can escape it using \
:
您可以使用\
:
var htmlAnch='<a onclick="javascript:selectEmployee(1100,\'Lauren O\'Donald\');return false;"
href="javascript:void(0);">O\'Donald, Lauren</a>';
However as you've tagged this question with jQuery,a better solution is to hook up an event to the element and use data-*
attributes to store the relevant information, which will avoid the use of ugly onX
attributes. Try this:
但是,当您使用 jQuery 标记此问题时,更好的解决方案是将事件连接到元素并使用data-*
属性来存储相关信息,这将避免使用丑陋的onX
属性。尝试这个:
var $htmlAnch = $('<a />' {
text: "O'Donald, Lauren" ,
data-id: 1100,
data-name: "Lauren O'Donald"
}).click(function(e) {
e.preventDefault();
selectEmployee($(this).data('id'), $(this).data('name'));
});
$(document).append($htmlAnch);
回答by rajesh kakawat
try something like this
尝试这样的事情
var htmlAnch='<a onclick="javascript:selectEmployee(1100,\'Lauren O\'Donald\');return false;" href="javascript:void(0);">O\'Donald, Lauren</a>';
回答by Shadow
Write your own function to return a escaped string. Demo
编写您自己的函数以返回转义字符串。演示
Pass your string as argument to this function and you will get escaped string. You can also add more characters to blocklist if you want to escape some more characters
将您的字符串作为参数传递给此函数,您将获得转义字符串。如果您想转义更多字符,您还可以向阻止列表添加更多字符
function remove_quotes(values1)
{
var values = values1.toString();
var str = "";
var blockList = ['"','\'','\']; // This is the list of key words to be escaped
var flag = 0;
for(var i = 0;i<values.length;i++)
{
for(var j=0;j<blockList.length;j++)
{
if(values[i] == blockList[j])
{
flag = 1;
break;
}
}
if(flag == 0)
str += values[i];
else
{
str += '\';
str += values[i];
flag = 0;
}
}
return str;
}
回答by Pankti Shah
In .aspx file you can do like as below:
在 .aspx 文件中,您可以执行如下操作:
<a data-open="editTemplateOverrideModal" onClick="populateUp
dateModal('<%#Server.HtmlEncode(Convert.ToString(DataBinder.Eval(Container.DataItem, "Description")).**Replace("'", "\'")) %>')**">