Jquery,如何转义引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6474958/
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
Jquery, how to escape quotes
提问by Sandro Antonucci
I'm using a simple jquery code that grabs html code form a tag and then puts this content into a form input
我正在使用一个简单的 jquery 代码,它从标签中获取 html 代码,然后将此内容放入表单输入中
<td class="name_cat" ><span class="name_cat">It's a "test" </span> (5)</td>
jquery gets the content into span.name_cat
and returns it as It's a "test"
.
So when I print this into an input it becomes
jquery 获取内容span.name_cat
并将其作为It's a "test"
. 因此,当我将其打印到输入中时,它会变成
<input value="It's a "test"" />
which as you can imagine will only show as It's a
, the following double quote will close the value tag.
What's the trick here to keep the original string while not showing utf8 code in the input?
正如您可以想象的那样It's a
,它只会显示为,下面的双引号将关闭 value 标签。在输入中不显示 utf8 代码的同时保留原始字符串的技巧是什么?
Jquery code
查询代码
$(".edit_cat").click(function(){
tr = $(this).parents("tr:first");
id_cat = $(this).attr("id");
td_name = tr.find(".name_cat");
span_name = tr.find("span.name_cat").html();
form = '<form action="/admin/controllers/edit_cat.php" method="post" >'+
'<input type="hidden" name="id_cat" value="'+id_cat+'" />'+
'<input type="text" name="name_cat" value="'+span_name+'" />'+
'<input type="submit" value="save" />'+
'</form>';
td_name.html(form);
console.log(span_name);
}
);
I basically need html()
not to decode Utf8
我基本上不需要html()
解码Utf8
回答by user113716
I believe this should take care of it for you.
我相信这应该为您解决。
var span_name = tr.find("span.name_cat").html().replace(/"/g, """);
Example:http://jsfiddle.net/yzthA/
示例:http : //jsfiddle.net/yzthA/
Also, don't forget to declare your variables with var
if you haven't.
另外,var
如果你还没有,不要忘记声明你的变量。
回答by Jim Schubert
You can build the form the way you are now, but traverse the form object using jQuery's context
您可以按照现在的方式构建表单,但使用 jQuery 的上下文遍历表单对象
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
<script type="text/javascript">
$(function() {
$('#go').click(function() {
var tester = $('<div></div>').html('It's a "test"');
var form = $('<form action="/admin/controllers/edit_cat.php" method="post" >' +
'<input type="hidden" name="id_cat" />' +
'<input type="text" name="name_cat" />' +
'<input type="submit" value="save" />' +
'</form>');
$(':hidden:first', form).attr('value', tester.text());
$(':text:first', form).attr('value', "Some attribute");
$('#output').append(form);
});
});
</script>
<input id="go" type="submit" value="click"/>
<div id="output">
</div>
What I've done here is wrapped your form html input in a jQuery object as form
. Then, you can query items within that object's context using the little-used jQuery form of:
我在这里所做的是将表单 html 输入包装在一个 jQuery 对象中作为form
. 然后,您可以使用很少使用的 jQuery 形式在该对象的上下文中查询项目:
jQuery( selector, [context] )
selector: string containing a selector expression
context: DOM Element, Document, or jQuery to use as context
jQuery( selector, [context] )
选择器:包含选择器表达式的字符串
上下文:用作上下文的 DOM 元素、文档或 jQuery
edit: I created a tester
object and set the content to your html example. Then, when I set the value in the hidden input, I call .text()
to get the non-html representation. The value is the same as you'd expect.
编辑:我创建了一个tester
对象并将内容设置为您的 html 示例。然后,当我在隐藏输入中设置值时,我调用.text()
以获取非 html 表示。该值与您期望的相同。
<form action="/admin/controllers/edit_cat.php" method="post">
<input type="hidden" name="id_cat" value="It's a "test"">
<input type="text" name="name_cat" value="Some attribute">
<input type="submit" value="save">
</form>
You'd have to decode this html on the backend, though. This is probably the best you'd be able to do because XML doesn't allow 'escaping' quotes like "\"this\""
. The specification requires quotes to be escaped like "this"
.
不过,您必须在后端解码此 html。这可能是您能做的最好的事情,因为 XML 不允许像"\"this\""
. 该规范要求像"this"
.
回答by spb
You can HTML encode the values with this function:
您可以使用此函数对值进行 HTML 编码:
function htmlEncode(str) {
var div = document.createElement('div');
var txt = document.createTextNode(str);
div.appendChild(txt);
return div.innerHTML;
}
and then encode any values that can contain quotes:
然后对可以包含引号的任何值进行编码:
span_name = htmlEncode(tr.find("span.name_cat").html());
A better alternative might be to build the inputs with jQuery and set the values with the val() method and then append to the form:
更好的选择可能是使用 jQuery 构建输入并使用 val() 方法设置值,然后附加到表单:
var $form = $('<form action="/admin/controllers/edit_cat.php" method="post" />');
var $id_cat_input = $('<input type="hidden" name="id_cat" />');
$id_cat_input.val(id_cat);
$id_cat_input.appendTo($form);
回答by Mr. Polywhirl
I converted user113716's answerinto a jQuery plugin and cleaned it up a bit.
我将 user113716 的答案转换为一个 jQuery 插件并对其进行了一些清理。
(function($) {
$.fn.escapeQuotes = function() {
return this.html().replace(/"/g, '"');
}
})(jQuery);
var spanValue = $('span').escapeQuotes();
var formTemplate = [
'<form action="/admin/controllers/edit_cat.php" method="post" >',
'<input type="text" name="name_cat" value="' + spanValue + '" />',
'<input type="submit" value="save" />',
'</form>'
].join('');
$(formTemplate).appendTo('body');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="name_cat">It's a "test" </span> (5)
回答by gandil
You should convert them to html entities or skip them. A quote as a html entity looks like this: " Do it with your programming language. Not with js.
您应该将它们转换为 html 实体或跳过它们。作为 html 实体的引用看起来像这样:“用你的编程语言来做。而不是用 js。