【问题标题】:Values selected in select box cannot be retrieved- jQuery无法检索在选择框中选择的值 - jQuery
【发布时间】:2016-06-14 11:26:01
【问题描述】:

我正在构建一个应用程序,其中我有一个表格元素,如选择、输入等。我想将表格元素中选择的值填充到另一个表格中。

你可以找到我的代码here

JS:

 $('button').click(function() {
  var rows = $('#table1 tbody tr');
  var previewTable = $('#table2 tbody');
  previewTable.find('tr').remove();
  for (var i = 0; i < rows.length; i++) {
   var tr = $('<tr> </tr>');
   previewTable.append(tr);
   var row_cloned = $(rows[i]).clone();
   var cols = rows[i].getElementsByTagName("td");
   for (var j = 0; j < cols.length; j++) {
    var col_cloned = row_cloned.find('td').clone();
    previewTable.find('tr').eq(i).append(col_cloned[j]);


    if ($(col_cloned[j]).children().length > 0) {
     $(col_cloned[j]).children().each(function(index, item) {
      if ($(item).is('select')) {
        if ($(item).attr('multiple')) {
          var foo = [];
          $(item).each(function(i, selected) {
            console.log($(selected).val());
            foo[i] = $(selected).val();
          });
          $(col_cloned[j]).text(foo);
        } else {
          $(col_cloned[j]).text($(item).val());
        }

      } else if ($(item).is('label')) {
        var selected = [];
        $(item).find('input:checked').each(function(index, item) {
          selected.push($(item).val());
          $(col_cloned[j]).append(selected + '<br>');
        })
      }
    })
  } else {
    $(col_cloned[j]).text($(col_cloned[j]).text());
    }
   }
  }
})

我的步骤:

  1. 获取table1和table2
  2. 统计table1的行数
  3. 在 table2 中添加这么多空行
  4. 获取table1中的每一行
  5. 统计table1每一行的td
  6. 在每个 td 中找到孩子
  7. 检查孩子是选择、输入还是纯文本
  8. 如果children是select,判断是多选还是单选
  9. 对每个表单元素采取相应措施,仅复制值,然后附加到 table2
  10. 完成

这一切都在一个按钮上点击复制

问题:不知何故设法获取检查的输入表单元素值。但未能在选择框中选择值。

对于多选框我的代码:

if ($(item).attr('multiple')) {
      var foo = [];
      $(item).each(function(i, selected) {
        console.log($(selected).val());
        foo[i] = $(selected).val();
      });
      $(col_cloned[j]).text(foo);
    }

对于单选框:

else {
      $(col_cloned[j]).text($(item).val());
    }

我的错误到底是什么?提前致谢

【问题讨论】:

  • 嗨,单元格输入类型是固定的吗?我的意思是,例如选择将始终到第一个单元格?
  • 是的,单元格表单字段是固定的。
  • @Legionar:您指向的链接是完全不同的场景。而且,我也尝试了那里提供的可能解决方案。
  • 检查我答案中的代码,它会起作用。

标签: javascript jquery


【解决方案1】:

对于选定的.val() 不起作用。您必须使用find(),因为当您选择一个选择框时,它的值不会改变。

 if ($(item).attr('multiple')) {
     var foo = [];
     $(item).each(function(i, selected) {
     foo[i] = $(selected).find(":selected").text();  // see this
     });
     $(col_cloned[j]).text(foo);
  } else {
     $(col_cloned[j]).text($(item).find(":selected").text());  // see this
  }

【讨论】:

    【解决方案2】:

    对于&lt;select&gt;,您不能使用val()

    在多选列表上使用.val() 函数将返回一个包含所选值的数组:

    这段代码:

    if ($(item).is('select')) {
        if ($(item).attr('multiple')) {
          var foo = [];
          $(item).each(function(i, selected) {
            console.log($(selected).val());
            foo[i] = $(selected).val();
          });
          $(col_cloned[j]).text(foo);
        } else {
          $(col_cloned[j]).text($(item).val());
        }
    
      }
    

    改成这样:

    if ($(item).is('select')) {
        if ($(item).attr('multiple')) {
          var foo = $('#multipleSelect').val();
          $(col_cloned[j]).text(foo);
        } else {
          var optionSelected = $(item).find("option:selected");
          $(col_cloned[j]).text(optionSelected.val());
        }
    
      }
    

    【讨论】:

    • 问题出在哪里?
    • 解释“这行不通”!它必须给出任何错误或警告...您已在浏览器控制台中调试它
    • 多选框的值为空,单选则取第一个选项。没有错误或没有警告
    • 查看我的编辑。现在好吗? $(col_cloned[j]).text(foo); 这会将文本更改为数组中的对象列表...你确定,你需要它吗?
    • 这不起作用,对于单选,仍然是第一个选项,对于多选,它将整个选择框复制到 table2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多