Jquery 如果我调用此方法,页面将崩溃

Jquery 如果我调用此方法,页面将崩溃,jquery,Jquery,我用这种方法随机回答一张表格 $('table').filter(function () { return ($(this).find('input').size() > 0); }).find('tr').each(function () { $(this).find('input').filter(function () { return (Math.round(Math.random()) == 1); }).each(

我用这种方法随机回答一张表格

  $('table').filter(function () {
      return ($(this).find('input').size() > 0);
  }).find('tr').each(function () {
      $(this).find('input').filter(function () {
          return (Math.round(Math.random()) == 1);
      }).each(function () {
          var typ = this.type;
          if (typ == 'checkbox' || typ == 'radio') {
              this.checked = true;
          }
          if (typ == 'select' || typ == 'select-one') {
              var value = $.map(this, function (elt, i) {
                  return $(elt).val();
              }).sort()[0];
              this.val(value);
          }
          if (typ = "text" || typ == "textarea") {
              $(this).val(Math.floor(Math.random() * 11));
          }
      });
  });
标签没有任何选择类型,您不能通过这种方式设置所选值。ValvValue;作为一个jQuery函数,请尝试$this.valvalue作为输入。如果要在其选项中选择一个,则必须将slector更改为包含select元素

      if (typ = "text" || typ == "textarea") {
          $(this).val(Math.floor(Math.random() * 11));
      }
通过使用single=而不是==可以重新声明使条件为true的变量

您还应该能够通过不使用find和each来缩短代码。下面的选择器使用表中的in-tr元素选择输入元素,然后像示例中那样随机筛选它

$('table tr input').filter(function() { return (Math.round(Math.random()) == 1); })

你说页面崩溃是什么意思?是的,我知道它应该在做什么。它实际上在做什么?在调用$'forwardbutton'。触发'click';,之后是否会发生上述崩溃;,还是更早?另外,请处理代码格式-由于缩进不正确,代码现在几乎不可读。我正在使用谷歌浏览器。。它要求我等待或终止页面。您对$.map的调用正在迭代当前元素的所有属性,而不是其子元素。由于DOM元素可以有许多属性,因此性能问题可能来自于此。
$('table tr input').filter(function() { return (Math.round(Math.random()) == 1); })