0

私は次のhtmlを持っています

<div class="ui-widget" style="margin-top: 1em; font-family: Arial">
   Selected:
<div id="locationLog" style="height: 100px; width: 200px; overflow: auto;" class="ui-widget-content"></div>
</div>

<input type="hidden" name="HiddenLocations" id="HiddenLocation" />

次に、ページが読み込まれるたびに実行されて、locationLogdivにテキストを追加するコードがあります。

@if (Model.SearchCriteria != null && Model.SearchCriteria.Locations != null)
{
   foreach (string t in @Model.SearchCriteria.Locations)
   {
      <script type="text/javascript">
         $("<div/>").text('@t').appendTo("#locationLog");
         $("<br/>").text("").appendTo("#locationLog");
         $("#locationLog").scrollTop(0);

         selectedLocations = $('#locationLog' + ' div').map(function () {
            return $(this).text();
         }).get();

         $('input[name=HiddenLocations]').val(selectedLocations); 
      </script>
   }
}

これで、このコードは「ロンドン」などのテキストがある場合はうまく機能しますが、「レイキャビク」がある場合は、「」と記述されReykjav&#237;kます。どうすれば正しいテキストを書き出すことができますか?

4

1 に答える 1

1

ドキュメントでutf-8文字セットを宣言してみてください

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

編集: これを試してください:

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

ここで使用します:

selectedLocations = $('#locationLog' + ' div').map(function () {
   return htmlDecode($(this).text());
}).get();

出典:JavascriptでHTMLエンティティをエスケープ解除しますか?

于 2012-09-19T10:26:11.853 に答える