jquery - 使用 jQuery 使用数组的第 n 个值填充第 n 个表单元素

标签 jquery html arrays

我想知道如何使用我在下面使用 jQuery 创建的数组的第 n 个值来填充第 n 个隐藏字段。我已经成功地创建了一个数组,它将括号中的 3 个复选框选项(如果它们被选中)分组到数组中。例如:[(A,B),(A,C),(A,B,C)...]。

我现在需要获取数组中的第 n 个值,并用该值填充第 n 个隐藏字段。

    <input type="hidden" name="opt_0" id="opt_0" class="get_options">
    <input type="hidden" name="opt_1" id="opt_1" class="get_options">
    <table>
        <tbody>
            <tr class="registrant">
                <td><input type="text" id="OETIPC-NAME0" name="OETIPC-NAME0"></td>
                <td><input type="checkbox" name="OETIPC-A0" id="OETIPC-A0" class="reg_type" value="A"></td>
                <td><input type="checkbox" name="OETIPC-B0" id="OETIPC-B0" class="reg_type" value="B"></td>
                <td><input type="checkbox" name="OETIPC-C0" id="OETIPC-C0" class="reg_type" value="C"></td>
            </tr>
            <tr class="registrant">
                <td><input type="text" id="OETIPC-NAME1" name="OETIPC-NAME1"></td>
                <td><input type="checkbox" name="OETIPC-A1" id="OETIPC-A1" class="reg_type" value="A"></td>
                <td><input type="checkbox" name="OETIPC-B1" id="OETIPC-B1" class="reg_type" value="B"></td>
                <td><input type="checkbox" name="OETIPC-C0" id="OETIPC-C1" class="reg_type" value="C"></td>
            </tr>
        </tbody>
    </table>

// Get registration option values and group for each row then create array
$('.registrant').each(function() {
  var attendee_reg_type = $(this).find('.reg_type:checked').map(function() {
    return this.value;
  }).get();
});

我现在需要输出类似于以下内容的内容:

    <input type="hidden" name="opt_0" id="opt_0" class="get_options" value="(A,B)">
    <input type="hidden" name="opt_1" id="opt_1" class="get_options" value="(A,C)">
    ...

有人可以帮忙吗?我对执行此操作的最佳方法有点迷茫。我确实尝试了以下方法,但没有成功:

$('.registrant').each(function() {
  var attendee_reg_type = $(this).find('.reg_type:checked').map(function() {
    return this.value;
  }).get();
  $('#opt_[i]').val(attendee_reg_type[i]);
});

非常感谢您的帮助!

最佳答案

为什么不简单地删除隐藏字段并像这样重命名您的复选框:

    <table>
        <tbody>
            <tr>
                <td><input type="text" id="OETIPC-NAME0" name="OETIPC-NAME0"></td>
                <td><input type="checkbox" name="opt_0" id="OETIPC-A0" class="reg_type" value="A"></td>
                <td><input type="checkbox" name="opt_0" id="OETIPC-B0" class="reg_type" value="B"></td>
                <td><input type="checkbox" name="opt_0" id="OETIPC-C0" class="reg_type" value="C"></td>
            </tr>
            <tr>
                <td><input type="text" id="OETIPC-NAME1" name="OETIPC-NAME1"></td>
                <td><input type="checkbox" name="opt_1" id="OETIPC-A1" class="reg_type" value="A"></td>
                <td><input type="checkbox" name="opt_1" id="OETIPC-B1" class="reg_type" value="B"></td>
                <td><input type="checkbox" name="opt_1" id="OETIPC-C1" class="reg_type" value="C"></td>
            </tr>
        </tbody>
    </table>

现在在提交时,您将得到检查值,在 opt_0opt_1 键中用逗号分隔。

jQuery 解决方案:

$('.registrant').each(function (index) {
    var attendee_reg_type = $(this).find('.reg_type:checked').map(function () {
        return this.value;
    }).get().join(",");
    $("#opt_" + index).val(attendee_reg_type);
});

关于jquery - 使用 jQuery 使用数组的第 n 个值填充第 n 个表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26517755/

相关文章:

python - 重新排序一个numpy数组python

java - 如何使用具有动态 id 的 JsonArray 解析 jsonObject 作为整数开始

jQuery 显示具有特定类的元素

javascript - Jquery Flot 条形图不适合板子

jquery - 我如何更改jquery中新添加行的行背景颜色

javascript - 仅当数据来自服务器时,Firefox 的 HTML5 下载问题

c - int 中使用前导零

javascript - 基于 CSS 类在文本区域内动态分配字符限制

javascript - 如何更改外部 .js 文件中的变量数据值?

html - 覆盖 Bootstrap 默认值 !important 颜色代码