javascript - jQuery 巨大的选择框导致 Uncaught RangeError : Maximum call stack size exceeded

标签 javascript jquery html google-chrome drop-down-menu

我有一个选择框,在选择“其他”选项后,将显示第二个选择框(超过 10k 的巨大列表)。这在 IE 和 Mozilla 中运行良好,但在 Chrome 中会遇到未捕获的 rangeError。我该如何调试和解决这个问题?

<!DOCTYPE html>
<html>

<body>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>
      $(document).ready(function() {

        $('#thisIsTheSelectBox').change(function() {

          var otherSelectBoxId = $(this).attr('id') + "xyz123";

          if ($(this).val() === "Others") {
            var otherSelectBox = "<select id='" + otherSelectBoxId + "' name='' unit='' uniquename='' class=' ' required='true'> " +
              "<option value='option 1'>option 1</option>" +
              "<option value='option 2'>option 2</option>" + 
              ...
              "<option value='option 10000'>option 10000</option>" + 
              "</select>";
            $(this).after(otherSelectBox);
          } else {
            $("#" + otherSelectBoxId).remove();
          }
        });

      });
    </script>
  </head>
  <table>
    <tr>
      <td width="330" valign="top">
        <select id="thisIsTheSelectBox" name="" required="true">
          <option value=''>-- Options --</option>
          <option value='1'>test1</option>
          <option value='2'>test2</option>
          <option value='Others'>Others</option>
        </select>
      </td>
    </tr>
  </table>
</body>

最佳答案

这可能是内部错误。您可以在一个循环中添加每个元素,而不是构建一个长字符串,这样代码会更轻便且更易于维护。

代码:

$(document).ready(function () {

    $('#thisIsTheSelectBox').change(function () {

        var otherSelectBoxId = $(this).attr('id') + "xyz123";

        if ($(this).val() === "Others") {
            var otherSelectBox = "<select id='" + otherSelectBoxId + "' name='' unit='' uniquename='' class=' ' required='true'> ";
            for (var i = 0; i <= 10000; i++) {
                otherSelectBox += "<option value='option" + i + "'>option " + i + "</option>";
            }
            otherSelectBox += "</select>";
            $(this).after(otherSelectBox);
        } else {
            $("#" + otherSelectBoxId).remove();
        }
    });

});

演示:http://jsfiddle.net/IrvinDominin/4gk5zd5t/

关于javascript - jQuery 巨大的选择框导致 Uncaught RangeError : Maximum call stack size exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28494063/

相关文章:

javascript - React 无法读取未定义的属性 'length' 但数组已定义

javascript - 保持 Bootstrap 侧边菜单可见

javascript - document.getElementById 不适用于动态 div

javascript - 如何将基于输入键的事件分配给 <ul> 中选定的 <li> 元素

javascript - 更新父元素jquery的类

javascript - REACT 动态注入(inject) SVG。 react svg

jQuery tablesorter、staticwidget 和 sortList 一起使用吗?

javascript - 如何让这个网格发挥作用

html - 100% 高度绝对定位的 div,带有粘性页脚

html - DIV 宽度 : auto is 0 pixel