jQuery 数据表中不允许小于 (<) 符号
Lessthan(<) symbol is not allowed in jQuery Datatable
要求不高,我想用JSON数据显示数据表,
在这里我有一个小挑战,如果 JSON 数据有特殊字符,如 lessthan symbol(<)
,数据不会显示在数据网格中。
不知道为什么只有 lessthan symbol(<)
才会出现问题
我尝试使用以下代码,如果遗漏任何内容,请纠正我,
Eg:last 名字是 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>"));
}
谢谢
因为<在HTML中是一个特殊字符,你需要转义它。
row.append($("<td>").text(rowData.firstName));
您可以使用
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.
要求不高,我想用JSON数据显示数据表,
在这里我有一个小挑战,如果 JSON 数据有特殊字符,如 lessthan symbol(<)
,数据不会显示在数据网格中。
不知道为什么只有 lessthan symbol(<)
才会出现问题
我尝试使用以下代码,如果遗漏任何内容,请纠正我,
Eg:last 名字是 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>"));
}
谢谢
因为<在HTML中是一个特殊字符,你需要转义它。
row.append($("<td>").text(rowData.firstName));
您可以使用
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.