简体   繁体   English

将带有单引号和双引号的值附加到文本框

[英]append values with both single and double quotes to textbox

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 .我需要将结果数据集中的一个值附加到可能具有任何特殊字符的input ,例如*^&$#>,'"等。问题是我无法将整个var附加到input

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情况下做到这一点吗?

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. 您应该使用.val设置值,以便正确转义该值。 Also, the regex is unnecessary. 此外,正则表达式是不必要的。

$(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上的更新测试用例

So to solve this problem without using .find or appending the var seperately, I found a simple solution for this. 所以为了解决这个问题而不使用.find或单独附加var ,我找到了一个简单的解决方案。 I just need to replace the single quote " ' " with its iso latin code &#39; 我只需要用它的iso latin代码替换single quote'&#39; and other special characters with their respective codes and then use that variable in .html 和其他特殊字符及其各自的代码,然后在.html使用该变量

var something= "this is vicky\"s \"s 's 's \"s \"S";
test=something.replace(/'/g,"&#39;").replace(/"/g,'\\"');
$("#test").html("hey this is <input id='testbox' value='"+test+"'></input>");

Have a look at this jsfiddle 看看这个jsfiddle

I tried so many things finally this worked:我尝试了很多事情,最后这奏效了:

var getClinician = row.ClinicianName;
getClinician = getClinician.replace(/"/g, "&quot;");

var Clinician = '<td align=""><input type="text" id="txt_Clinician_' + row.ScheduleID + '" value="' + getClinician + '"  />';

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

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