I need to append a value from the result data set to a input
that may have any special character like *^&$#>,'"
etc. The problem is that I am not able to append the entire var
into the input
.
我需要添加一个值从结果数据集可能有任何特殊字符的输入像* ^ & $ # >”等等。问题是,我不能够将整个var的输入。
Please check this fiddle. What is the thing that I have missed out in it?
请检查这个小提琴。我错过了什么?
Cant I do this without using .find
and .append
like this one?
我不可以不使用。find和。append like this ?
2 个解决方案
#1
3
This is because you are concatenating strings, which doesn't take escaping of special characters into account. You should instead set the value with .val
so that the value is escaped properly. Also, the regex is unnecessary.
这是因为您正在连接字符串,不考虑特殊字符的转义。您应该使用.val来设置值,以便正确地转义该值。而且,regex也是不必要的。
$(document).ready(function(){
var test= "this is vicky\"s \"s 's 's \"s \"S";
alert(test);
var input = $("<input style='width:700px;' id='testbox'></input>").val(test);
$("#test").html("hey this is ").append(input);
});
See updated test case on jsFiddle
参见jsFiddle的更新测试用例。
#2
2
So to solve this problem without using .find
or appending the var
seperately, I found a simple solution for this. I just need to replace the single quote
" ' " with its iso latin code '
and other special characters with their respective codes and then use that variable in .html
为了解决这个问题而不使用。find或单独附加var,我找到了一个简单的解决方案。我只需要用它的iso拉丁代码和#39替换单引号“'”;以及其他具有各自代码的特殊字符,然后在.html中使用该变量
var something= "this is vicky\"s \"s 's 's \"s \"S";
test=something.replace(/'/g,"'").replace(/"/g,'\\"');
$("#test").html("hey this is <input id='testbox' value='"+test+"'></input>");
Have a look at this jsfiddle
看一下这张拉小提琴?
#1
3
This is because you are concatenating strings, which doesn't take escaping of special characters into account. You should instead set the value with .val
so that the value is escaped properly. Also, the regex is unnecessary.
这是因为您正在连接字符串,不考虑特殊字符的转义。您应该使用.val来设置值,以便正确地转义该值。而且,regex也是不必要的。
$(document).ready(function(){
var test= "this is vicky\"s \"s 's 's \"s \"S";
alert(test);
var input = $("<input style='width:700px;' id='testbox'></input>").val(test);
$("#test").html("hey this is ").append(input);
});
See updated test case on jsFiddle
参见jsFiddle的更新测试用例。
#2
2
So to solve this problem without using .find
or appending the var
seperately, I found a simple solution for this. I just need to replace the single quote
" ' " with its iso latin code '
and other special characters with their respective codes and then use that variable in .html
为了解决这个问题而不使用。find或单独附加var,我找到了一个简单的解决方案。我只需要用它的iso拉丁代码和#39替换单引号“'”;以及其他具有各自代码的特殊字符,然后在.html中使用该变量
var something= "this is vicky\"s \"s 's 's \"s \"S";
test=something.replace(/'/g,"'").replace(/"/g,'\\"');
$("#test").html("hey this is <input id='testbox' value='"+test+"'></input>");
Have a look at this jsfiddle
看一下这张拉小提琴?