From my API I get a json object with data. The special data is converted to HTML entities (using htmlspecialchars). Looks like this:
{
id: "0",
title: "Hello & good day",
}
However, when i fetch the data using $.getJSON, and insert the data into the input value, the &
is still there.
How can I avoid this?
You can create a dummy element to pass the HTML entity in as the dummy element's html
, then pass the text()
of that dummy element to the input
element's value:
var $dummy = $('<div/>', { html: 'Hello & good day'});
$('input').val($dummy.text());
In one line:
$('input').val($('<div/>', { html: 'Hello & good day'}).text());
If you are able to change the PHP to encode the text using rawurlencode , you can decode the string before setting it as the input value using:
input.value = decodeURIComponent(jsonData.title);
(assuming that jsonData.title
is the encoded string from the response.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.