[英]How to pass quotation marks to javascript function
我有一個javascript函數,它接受包含引號的字符串並在輸入字段中顯示。 但是這個函數不接受引號后面的字符串
function searchFormatter(cellvalue, options, rowObject) {
return '<input id="txt' + cellvalue + '" type="text" disabled="disabled" value="' + cellvalue + '" />';
}
例如。 21“英寸==> 21
無論如何,我可以將引號傳遞給函數並打印它們。 我不想替換它們。 我想要顯示它們。
您必須轉義特殊的HTML字符,例如"
, <
, >
。例如:
function searchFormatter(cellvalue, options, rowObject) {
cellvalue = cellvalue.
replace(/&/g,'&').
replace(/>/g,'>').
replace(/</g,'<').
replace(/"/g,'"');
return '<input id="txt' + cellvalue + '" type="text" disabled="disabled" value="' + cellvalue + '" />';
}
你必須傳遞cellValue的值,如下所示(var cellValue = 21“(應該給出21后跟”后面跟着;(分號))
你可以在輸入框中得到21“的值。希望這會有所幫助。
你需要替換“with&ampquot;
使用此輔助方法:
function htmlEncodeInputDisplay(string) {
if (string == null || jQuery.trim(string) == "") return string;
string = string.replace(/</g, "<");
string = string.replace(/>/g, ">");
string = string.replace(/&/g, "&");
string = string.replace(/ /g, " ");
string = string.replace(/\"/g, """);
return string;
}
您可以使用Javascript函數escape()對sting進行編碼,然后使用unescape()在需要時對其進行解碼。
您也可以手動轉義它們,如下例所示。
var name = prompt("Please write your \'name\' in the box\r Write it with \"quotes\" like this.","");
使用\\轉義的報價作為字符串的一部分進行。
只是在創建輸入元素時轉義該值,然后取消該值以正確顯示它。
簡化示例:
function make_input(val) {
return '<input value="' + escape(val) + '" />';
}
// add to dom
$('input').each(function(){
var current_value = $(this).val()
var new_value = unescape(current_value)
$(this).val(new_value)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.