简体   繁体   English

JavaScript无法处理瑞典语字母

[英]JavaScript is not working on Swedish letters

My javascript code for filtering is not working on Swedish letters å ä ö 我的JavaScript过滤代码不适用于瑞典语字母åäö

<input name='tablefilter' type='checkbox' value='Linköping' id='tablefilter1' checked/>
<label for='tablefilter1'>Linköping</label>
<input name='tablefilter' type='checkbox' value='Mjölby' id='tablefilter2' checked/>
<label for='tablefilter2'>Mjölby</label>
<input name='tablefilter' type='checkbox' value='Norrköping' id='tablefilter3' checked/>
<label for='tablefilter3'>Norrköping</label>
<table>
  <thead>
    <tr>
      <th>Col1</th>
      <th>Col2</th>
      <th>Col3</th>
    </tr>
  </thead>
  <tbody id='tablebody'>
    <tr>
      <td>Linköping</td>
      <td>One</td>
      <td>First</td>
    </tr>
    <tr>
      <td>Mjölby</td>
      <td>Two</td>
      <td>Second</td>
    </tr>
    <tr>
      <td>Norrköping</td>
      <td>Three</td>
      <td>Third</td>
    </tr>
  </tbody>
</table>

js js

/* Demo filtering table using checkboxes. Filters against first td value */

/* Set 'ready' handler' */
document.addEventListener('DOMContentLoaded', initFunc);

/* When document ready, set click handlers for the filter boxes */
function initFunc(event) {
  var filters = document.getElementsByName('tablefilter');
  for (var i = 0; i < filters.length; i++) {
    filters[i].addEventListener('click', buildAndExecFilter);
  }
}

/*
    This function gets called when clicking on table filter checkboxes.
    It builds a list of selected values and then filters the table based on that
*/
function buildAndExecFilter() {
  var show = [];
  var filters = document.getElementsByName('tablefilter');
  for (var i = 0; i < filters.length; i++) {
    if (filters[i].checked) {
      show.push(filters[i].value);
    }
  }
  execFilter(show); // Filter based on selected values
}

function execFilter(show) {
  /* For all rows of table, see if td 0 contains a selected value to filter */
  var rows = document.getElementById('tablebody').getElementsByTagName('tr');
  for (var i = 0; i < rows.length; i++) {
    var display = ""; // Default to display
    // If it is not found in the selected filter values, don't show it
    if (show.indexOf(rows[i].children[0].textContent) === -1) {
      display = "none";
    }
    // Update the display accordingly
    rows[i].style.display = display;
  }
}

http://jsfiddle.net/2Lm7pytt/4/ http://jsfiddle.net/2Lm7pytt/4/

It works on jsfiddle, but it's not working on my visual studio. 它适用于jsfiddle,但不适用于我的Visual Studio。 The problem is not that it can't display Swedish letters, because it can. 问题在于它不能显示瑞典字母,因为它可以。

The problem is that the JavaScript is not working on Swedish letters. 问题在于JavaScript无法在瑞典语字母上运行。

What should I do to make it work? 我应该怎么做才能使其正常工作?

you might try adding to the top of the head tag 您可以尝试将其添加到head标签的顶部

< meta charset="utf-8" /> <meta charset =“ utf-8” />

This could fail at several points but it is most likely failing on the comparison due to an encoding issue, so I would suggest using the debugger to breakpoint the JavaScript before this line: 这可能会在某些时候失败,但是由于编码问题,它很可能在比较中失败,因此我建议在此行之前使用调试器对JavaScript进行断点:

if (show.indexOf(rows[i].children[0].textContent) === -1) {
     display = "none";
}

Check what is contained within textContent , try doing a comparison in your Immediate Window, eg rows[i].children[0].textContent === "Linköping" . 检查textContent包含的内容,尝试在“即时窗口”中进行比较,例如, rows[i].children[0].textContent === "Linköping" If you find this coming back false, this will likely confirm it. 如果您发现此错误返回,则可能会确认。

Go to FILE > Advanced Save Options in Visual Studio and check the encoding option set here - I am using "Unicode (UTF-8 with signature) - Codepage 65001" and my code includes the meta code as seen in Brandon's suggestion in the answers below. 转到Visual Studio中的“文件”>“高级保存选项”,然后在此处检查设置的编码选项-我使用的是“ Unicode(带签名的UTF-8)-代码页65001”,我的代码包含元数据,如下面答案中的布兰登建议所示。 With these set, it is running as intended, for me. 有了这些设置,它对我来说正在按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM