English 中文(简体)
jquery自动完成:如何处理特殊字符(&和)?
原标题:
  • 时间:2009-04-08 13:20:12
  •  标签:

While using jQuery s autocomplete, I ve noticed that characters & and are escaped as & and '

Example, autocomplete displays Barbara’s Straight Son but when I choose this the field shows Barbara's Straight Son.

有什么办法可以避免这种情况吗?

谢谢你抽出时间!

最佳回答

您需要取消对html实体的捕获。我发现最简单的方法就是使用这个函数:

var unescapeHtml = function (html) {
      var temp = document.createElement("div");
      temp.innerHTML = html;
      var result = temp.childNodes[0].nodeValue;
      temp.removeChild(temp.firstChild)
      return result;
  }

因此,在将字符串放入输入框之前,请先通过此函数。为了做到这一点,您可能需要修改自动完成插件。

问题回答

例如,您可以使用以下代码在自动完成中设置标签值

var temp = document.createElement("pre"); // create new DOM element 
temp.innerHTML = "Barbara's Straight Son" ; // set string which content HTML entities 
return {
            label: temp.firstChild.nodeValue, // get string from that DOM element which replace HTML entities to special character 
            value: "" // set your value here
}




相关问题
热门标签