簡體   English   中英

將帶有單引號和雙引號的值附加到文本框

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

我需要將結果數據集中的一個值附加到可能具有任何特殊字符的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 ,我找到了一個簡單的解決方案。 我只需要用它的iso latin代碼替換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 + '"  />';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM