如何遍历表并在三个不同的数组中收集这些字段的值

How to iterate through table and collect in three different arrays values from that fields?

本文关键字:数组 字段 三个 遍历 何遍历      更新时间:2023-09-26

如何使用JQuery遍历表,并从这些字段中收集三个不同的数组值?我有类似的行

<tr>
    <td>
        <select id="name_x"></select>
    </td>
    <td>
        <select id="op_x"></select>
    </td>
    <td>
        <input type="text" id="val_x"/>
    </td>
</tr>

其中x在每一行中都不同(但都以name_、op_和val_开头)。如何收集?

http://api.jquery.com/attribute-starts-with-selector/

$('tr').each(function(i,el) {
    var $this = $(this);
    str1 = $this.find('select[id^="name_"]').val();
    str2 = $this.find('select[id^="op_"]').val();
    str3 = $this.find('input[id^="val_"]').val();
    // now do what you like with those strings
});