javascript - 从动态表中获取每个单元格的值

标签 javascript c# jquery

我有这样创建的动态表

<table id="sort" class="table">
                <thead>
                    <tr>
                        <th>Nazwa kolumny z BD*</th>
                        <th>Długość rekordu*</th>
                        <th>Nazwa kolumny wyświetlana</th>
                        <th>Format*</th>
                    </tr>
                </thead>
                <tbody id="tblsort">
                    @for (var i = 0; i < Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB.Count; i++)
                    {
                        <tr>
                            <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).ColumnNamesDB[i], new { @class = "form-control", })</td>
                            <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).LengthColumns[i], new { @class = "form-control" })</td>
                            <td>@Html.TextBoxFor(m => Model.listOfFormats(Model.field.TemplateName).ColumnNamesUser[i], new { @class = "form-control" })</td>
                            <td>@Html.DropDownListFor(m => Model.listOfFormats(Model.field.TemplateName).Formats[i], new SelectList(Model.formatFieldValues, "Value", "Text"), Model.listOfFormats(Model.field.TemplateName).Formats[i], new { @class = "form-control" })</td>
                        </tr>
                    }
                </tbody>
            </table>

如何从该表中的每个单元格中获取值?感谢您的帮助!

编辑:

好吧,我的意思是每个单元格数据。

我尝试了你的解决方案,但没有任何效果

这是我的.html,由浏览器生成

<tbody id="tblsort">
                        <tr>
                            <td name="cos"><input class="form-control" id="ColumnNamesDB_0_" name="ColumnNamesDB[0]" type="text" value="zzzz" /></td>
                            <td><input class="form-control" id="LengthColumns_0_" name="LengthColumns[0]" type="text" value="111" /></td>
                            <td><input class="form-control" id="ColumnNamesUser_0_" name="ColumnNamesUser[0]" type="text" value="zzzzz" /></td>
                            <td><select class="form-control" id="Formats_0_" name="Formats[0]"><option value=""></option>
<option value="DD-MM-YYY">DD-MM-YYY</option>
<option value="DDMMYYY">DDMMYYY</option>
<option value="NRB">NRB</option>
</select></td>
                            </tr>
                    </tbody>

当我运行此代码时:

 var value = $("#ColumnNamesDB[0]").attr('value');
    var bla = $('.ColumnNamesDB_0_').val();

    alert(bla);
    alert(value);

我得到了“未定义”。怎么了?

最佳答案

我不知道你所说的“值(value)”是什么意思,但你可以迭代每个 <td>使用 jQuery each()

$(function() {

  var myArray = [];

  $('#tblsort td input').each(function() {
    var input = $(this),
      value = input.attr('value');

    myArray.push(value);
  });
  console.log(myArray);
});

关于javascript - 从动态表中获取每个单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33342574/

相关文章:

Javascript:使用用户输入搜索数组数组

javascript - 在 HTML5/JS 中更改 SVG 圆的属性

c# - Sprite 批处理: "Begin cannot be called again until End has been successfully called."

c# - 更改 IQueryable 的 DataContext

php - 使用 Javascript 和 PHP 通过下拉表单在网页上加载 Google map

javascript - 音频标签,点击链接时添加src?

c# - ListBox ItemTemplate 中的按钮未选择项目

jquery - 如何使用 Spring Security 和 jQuery 处理过期 session ?

javascript - jQCloud 不生成任何元素

javascript - 使用 jQuery 在 html 数组中设置一个值