开发者

Force display text from Unicode in input field [duplicate]

开发者 https://www.devze.com 2023-01-17 00:01 出处:网络
This question already has answers here: How to decode HTML entities using jQuery? 开发者_运维知识库(20 answers)
This question already has answers here: How to decode HTML entities using jQuery? 开发者_运维知识库 (20 answers) Closed 8 years ago.

We are doing an AJAX call to retrieve from database. Since our clients may use different languages we encode everything into Unicode to store in the database (saves worrying about collations and such). Now when we fetch such content to be displayed in an input text field it is displaying the Unicode codes. Checked the HTML 4 documentation for input and value is a CDATA which tells me those unicode should be displayed as their character.

From the screen shot attached you can see this is not the case, I'm wondering if there is a way to "force" this behavior.

Force display text from Unicode in input field [duplicate]


Since our clients may use different languages we encode everything into ascii to store in the database (saves worrying about collations and such).

IMHO storing html entities into the database is a very bad approach. I would strongly recommend you using UTF-8 encoding everywhere. This is what will save you from worrying about collations and such.


You're passing a JavaScript string full of &#...; numeric character references. JavaScript strings are not HTML-encoded, so your code really does make a JS string containing an ampersand, a hash, and so on.

When you set it as an input's DOM value (val()) naturally those ampersands will still be there. DOM properties are plain strings, not markup. You only need to HTML-encode strings in JavaScript if you intend to make markup out of them for use with innerHTML/html().

So the PHP should not HTML-encode content that isn't going be to inserted into HTML. Use json_encode() to create JavaScript string literals:

$('#js_global_status_input').val(<?php echo json_encode($status_value); ?>);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号