简体   繁体   English

jQuery将多个(复选框的)检查操作值添加到输入文本

[英]JQuery Add Multiple Check Action Values (of Checkboxes) to Input Text

I have question quite similar to javascript-php-add-checkbox-values-to-input-text 我有非常类似于javascript-php-add-checkbox-values-to-input-text的问题

There is a parent checkbox, with 6 consecutive child checkbox. 有一个父复选框,其中有6个连续的子复选框。 I've created a script when child checkbox checked, parent checkbox will be checked to. 当选中子复选框时,我已经创建了一个脚本,父复选框也将被选中。 And if all 6 child checkboxes cleared, parent checkbox will be cleared also. 并且如果所有6个子复选框都被清除,则父复选框也将被清除。 Is there any way to input (and remove) these multiple check action from input text, since currently its only recognize one checkbox clicked. 有什么方法可以从输入文本中输入(或删除)这些多重检查操作,因为当前它只能识别单击的一个复选框。 Im using latest jquery. 我正在使用最新的jQuery。

http://jsfiddle.net/L33jo3j7/ http://jsfiddle.net/L33jo3j7/

HTML 的HTML

<input type="checkbox" name="chkcc5" id="chkGrpSD3" value="sd">SD
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 1">Kelas 1
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 2">Kelas 2
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 3">Kelas 3
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 4">Kelas 4
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 5">Kelas 5
<input type="checkbox" name="chk5[]" class="chkGrpSD3" value="kelas 6">Kelas 6
<input type="text" name="modelos" />

JQuery jQuery查询

$("input.chkGrpSD3").click(function () {
    var flag = $('#chkGrpSD3').is(':checked');

    if (flag === false) {
        $('#chkGrpSD3').prop('checked', true);
    }

    var noOneChecked = $('input[name="chk5[]"]:checked').length === 0;
    if (noOneChecked) {
        $('#chkGrpSD3').prop('checked', false);
    }
});

$("#chkGrpSD3").click(function () {
    if (!this.checked) {
        $('input.chkGrpSD3').prop('checked', false);
    }
});


$(":checkbox").click(function () {

    var delimiter = ";";
    var checkbox = $(this);
    var text = $("input[name='modelos']");

    if (checkbox.is(":checked")) {
        text.val(text.val() + checkbox.val() + delimiter)
    } else {
        text.val(function () {
            return $(this).val().replace(checkbox.val() + delimiter, "");
        });
    }
});

I was able to come up with a solution by simplifying your logic a little bit. 通过稍微简化您的逻辑,我提出了一个解决方案。 When setting the values in the textbox, it doesn't matter what checkbox was clicked, so we don't need to be concerned with that. 在文本框中设置值时,单击哪个复选框都没有关系,因此我们不必担心。 We just need to get all the checked checkboxes and put their values in the textbox. 我们只需要获取所有选中的复选框并将其值放在文本框中即可。 The following jQuery will get all the checked checkboxes on the page: $(":checked") . 以下jQuery将获取页面上所有选中的复选框: $(":checked") From there, you can just take the value of each one, add the delimiter, and append it to the textbox value. 从那里,您可以只取每个值,添加定界符,并将其附加到文本框值。 Here is an updated version of your fiddle to illustrate this: 这是您的小提琴的更新版本,以说明这一点:

http://jsfiddle.net/jwnace/ed23pkc4/ http://jsfiddle.net/jwnace/ed23pkc4/

EDIT: For the sake of clarity, here is the code that I added to the fiddle: 编辑:为清楚起见,这是我添加到小提琴中的代码:

var str = "";

// for each checked checkbox, add the checkbox value and delimiter to the textbox
$(":checked").each(function () {
    str += $(this).val() + delimiter;
});

// set the value of the textbox
text.val(str);

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

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