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

append values with both single and double quotes to textbox

本文关键字:文本 单引号      更新时间:2023-09-26

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

请检查一下这把小提琴。我遗漏了什么?

如果不像这样使用.find.append,我能做到这一点吗?

这是因为您正在连接字符串,而不考虑特殊字符的转义。您应该改为使用.val设置该值,以便正确转义该值。此外,正则表达式是不必要的。

$(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);
});

请参阅jsFiddle 上的更新测试用例

因此,为了在不使用.find或单独附加var的情况下解决此问题,我找到了一个简单的解决方案。我只需要将single quote"'"替换为其等拉丁代码&#39;,并将其他特殊字符替换为各自的代码,然后在.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>");

看看这个jsfiddle

我尝试了很多东西,终于成功了:

var getClinician = row.ClinicianName;
getClinician = getClinician.replace(/"/g, "&quot;");
var Clinician = '<td align=""><input type="text" id="txt_Clinician_' + row.ScheduleID + '" value="' + getClinician + '"  />';