有一个小要求,我想用 JSON 数据显示数据表,
在这里我有一个小挑战,如果 JSON 数据有特殊字符,如 lessthan symbol(<)
,数据不显示在数据网格中。
不知道为什么只有 lessthan symbol(<)
才会出现问题
我尝试使用以下代码,如果有任何遗漏,请纠正我,
例如:姓氏是 Jhons<asdf
但它只显示 Jhons
请帮我解决这个问题。
这是我的示例代码
$.ajax({
url: '/echo/json/',
type: "post",
dataType: "json",
data: {
json: JSON.stringify([
{
id: 1,
firstName: "Peter&heins",
lastName: "Jhons<asdf"},
{
id: 2,
firstName: "David>tyy",
lastName: "Bowie<wwww"},
{
id: 2,
firstName: "David<test",
lastName: "testqwwe>qewrqwe"}
]),
delay: 3
},
success: function(data, textStatus, jqXHR) {
// since we are using jQuery, you don't need to parse response
drawTable(data);
}
});
function drawTable(data) {
for (var i = 0; i < data.length; i++) {
drawRow(data[i]);
}
}
function drawRow(rowData) {
var row = $("<tr />")
$("#personDataTable").append(row); //this will append tr element to table... keep its reference for a while since we will add cels into it
row.append($("<td>" + rowData.id + "</td>"));
row.append($("<td>" + rowData.firstName + "</td>"));
row.append($("<td>" + rowData.lastName + "</td>"));
}
谢谢
最佳答案
<强> Demo
你可以使用
row.append($("<td>" + rowData.lastName.replace("<","<") + "</td>"));
代替
row.append($("<td>" + rowData.lastName + "</td>"));
If you use the less than (<) or greater than (>) signs in your text, the browser might mix them with tags. Character entities are used to display reserved characters in HTML.
<强> Manual
关于jquery - jQuery 数据表中不允许使用小于 (<) 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28577589/