繁体   English   中英

如何获取表格内的文本框值

[英]How to get textbox values inside the table

我的网页中有一个表格(带有两个文本框 Item 和 Count)。

在此处输入图片说明

我尝试访问每行中两个文本框的值。

请看下面我的代码

 <table id="tbl-contents" class="table">
    <thead>
        <tr>
            <th>Item</th>
            <th>Count</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <tr class="clsRow">
            <td>
                @*
                <input type="text" class="form-control" placeholder="Item" data-required="true">*@
                <input id="txtItem" class="form-control" placeholder="Item" type="text" value="" tabindex="-1" name="" data-required="true">
            </td>
            <td>
                @*
                <input type="text" class="form-control" placeholder="Item Count" data-required="true">*@
                <input id="txtItemCount" class="form-control" placeholder="Item Count" type="text" value="" tabindex="-1" name="" data-required="true">
            </td>
        </tr>
        <tr class="clsRow">
            <td>
                <input type="text" class="form-control" placeholder="Item">
            </td>
            <td>
                <input type="text" class="form-control" placeholder="Item Count">
            </td>
            <td>
                <a class="clsDelContent"><i class="fa fa-minus-square" style="font-size: 22px;"></i></a>
            </td>
        </tr>
        <tr class="clsRow">
            <td>
                <input type="text" class="form-control" placeholder="Item">
            </td>
            <td>
                <input type="text" class="form-control" placeholder="Item Count">
            </td>
            <td>
                <a class="clsDelContent"><i class="fa fa-minus-square" style="font-size: 22px;"></i></a>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3" style="text-align: right;">
                <a class="clsAddContent"><i class="fa fa-plus-square" style="font-size: 22px;"></i></a>
            </td>
        </tr>
    </tfoot>
</table>

脚本

        $('#tbl-contents tbody tr').each(function () {
            var customerId = $(this).find("td").html();



            alert(customerId);
        });
$('.clsRow').each(function() {
    var foo = $(this).find('input');
    var item = foo[0].value;
    var itemcount = foo[1].value;
})

JSFiddle

你可以试试:

$('table  tbody tr td input').each(function(){
    console.log($(this).val())
});

现场演示

您可以使用以下脚本。

var reqValues = [];
$('.clsRow').each(function(i){
   var $currentTD = $(this).find('td')
   reqValues["item"+i] = $currentTD.eq(0).find('input').val();
   reqValues["values"+i] = $currentTD.eq(1).find('input').val();
 });

要获得特定值,请使用以下代码 -

reqValues["item1"]
reqValues["values1"]
reqValues["item2"]
reqValues["values2"]

暂无
暂无

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

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