Javascript 从表的多tr中获取值

Javascript 从表的多tr中获取值,javascript,jquery,Javascript,Jquery,我有很多表行,我想得到它的值,它是一个数组中每行的数字 <form class="form-horizontal" id="form1" name="form1" method="post" action="url"> <table id="temp1" class="table datatable"> <thead> <tr> <th>Name</th>

我有很多表行,我想得到它的值,它是一个数组中每行的数字

<form class="form-horizontal" id="form1" name="form1" method="post" action="url">
<table id="temp1" class="table datatable">
    <thead>
        <tr>
            <th>Name</th>
            <th>Number</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>A</td>
            <td><input type="number" id="pduct1" class="product" name="pduct[]" /></td>
        </tr>
        <tr>
            <td>B</td>
            <td><input type="number" id="pduct2" class="product" name="pduct[]" /></td>
        </tr>
        <tr>
            <td>C</td>
            <td><input type="number" id="pduct3" class="product" name="pduct[]" /></td>
        </tr>
        <tr>
            <td>D</td>
            <td><input type="number" id="pduct4" class="product" name="pduct[]" /></td>
        </tr>                        
    </tbody>
</table>   

像这段代码一样,我想得到每个pduct的值

您可以使用$'temp1 input[name=pduct[]]选择有问题的元素-也就是说,使用元素的name属性,但仅限于temp1中的输入

要获取它们的所有值,可以使用返回新jQuery对象的,然后使用将其转换为数组:

var arr = $('#temp1 input[name="pduct[]"]').map(function() { return this.value }).get()
比如说,在点击按钮的情况下,您会看到如下内容:

$button.clickfunction{ var arr=$'temp1 input[name=pduct[]]'.mapfunction{return this.value}.get console.logJSON.stringifyarr } 获取价值 名称 数字 A. B C D 在输入标记中添加class属性。e、 g.等级=abc

使用Jquery

$('.abc').each(function(i, obj) {
    //$(this).val(); << return value
});
JQuery

您可以使用和获取表单的所有输入值

在您的示例中,您可以执行以下操作:

var formData=$'form1'。序列化数组\\输出形式为[{name,value}] var formArray=formData.mapfunctionelement{\\n如果您只需要这些值 返回元素.value; } 名称 数字 A. B C D
您的HTML无效:id元素应该是唯一的。您仍然可以将值放入数组中,但无论如何都应该修复ID。@nnnnnn那么我该怎么做呢?请帮助了解要获取输入值的事件。。使用类而不是ID