简体   繁体   English

将多个选定复选框的值传递到多个相应的输入字段中

[英]Pass value of multiple selected checkboxes into multiple corresponding input fields

I have this code: 我有以下代码:

<label><input type="checkbox" />A</label><br />
<label><input type="checkbox" />B</label><br />
<label><input type="checkbox" />C</label><br />
<label><input type="checkbox" />D</label><br />

<input name="selected_1" />
<input name="selected_2" />
<input name="selected_3" />

$(function() {
    $('input').on('click', function() {
        var values = [];
        $('input:checked').each(function() {
            values.push($(this).parent().text());
        });
        $('[name="selected_1"]').attr({value: values.join(', ')});
    });
});

The code above accurately passes the value of all selected checkboxes (comma-separated) to the input field [selected_1]. 上面的代码将所有选中的复选框的值(逗号分隔)准确地传递到输入字段[selected_1]。

However, I want to pass the value of any of the selected checkbox into [name="selected_1"], the second selected checkbox value into [name="selected_2"] and the third selected checkbox to [name="selected_3"] in no given order. 但是,我想将任何选定复选框的值传递给[name =“ selected_1”],第二个选定复选框的值传递给[name =“ selected_2”],第三个选定复选框的值传递给[name =“ selected_3”]没有给定的顺序。 Then I want to limit the selection to a maximum of only three check-boxes selected in the form. 然后,我想将选择范围限制为最多只有三个复选框。

This fiddle Limit Checkbox Selection Demonstrates how limiting the selection is achieved. 限制复选框选择小提琴演示了如何限制选择。

Now, how do I pass the value of each checkbox selected into the corresponding input field using Javascript or JQuery 3.3.1? 现在,如何使用Javascript或JQuery 3.3.1将选中的每个复选框的值传递到相应的输入字段中?

Any help will be greatly appreciated. 任何帮助将不胜感激。

You need to maintain some relationship between the checkboxes and inputs, so that when a checkbox is clicked, it's related input value is set, for this, you can use data attributes, like below 您需要保持复选框和输入之间的某种关系,以便在单击复选框时设置其相关的输入值,为此,您可以使用数据属性,如下所示

<label><input type="checkbox" data-input="selected_1" />A</label><br />
<label><input type="checkbox" data-input="selected_2"/>B</label><br />
<label><input type="checkbox" data-input="selected_3"/>C</label><br />
<label><input type="checkbox" data-input="selected_4"/>D</label><br />

<input name="selected_1" />
<input name="selected_2" />
<input name="selected_3" />

Now, onclick of checkbox, we can associate it with a textbox and set its value like below, 现在,单击复选框,我们可以将其与文本框相关联,并按如下所示设置其值,

$(function() {
    $('input[type="checkbox"]').on('click', function() {
        var values = [];
        $('input:checked').each(function() {
            values.push($(this).parent().text());
        });
        $('[name="'+$(this).attr("data-input")+'"]').attr({value: values.join(', ')});
    });
});

Working demo 工作演示

I'm not sure why you're doing values.join(', ') instead of setting just a single checkbox value 我不确定为什么要执行values.join(',')而不是仅设置单个复选框值

Working demo assigning single checkbox value to each input 工作演示为每个输入分配单个复选框值

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

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