jquery - 删除未选中复选框的值

标签 jquery checkbox

我有以下代码,它动态生成一个复选框,并在选择时将其值分配给变量。 我面临的问题是,一旦取消选中复选框,我不知道如何从变量中删除值。

function getCheckBox(contentItem) {
        var checkbox = $("<input type='checkbox'/>");
        checkbox.css("height", "20");
        checkbox.css("width", "20");

        checkbox.change(function () {
            var checked = checkbox[0].checked;

            $(':checkbox').change(function () {
                if ($(this).is(':checked'))
                {
                    contentItem.value.details.checked = true;
                    global_audit_team = global_audit_team + contentItem.value.Name + ",";
                    alert(global_audit_team);
                }

                else
                {
                    contentItem.value.details.checked = false;
                    alert(contentItem.value.Name + " unchecked");
                }
            });


        });
        return checkbox;
    };

最佳答案

您可以使用这个简单的 JQuery 代码。

var checkedCheckbox = "";
$("input[type='checkbox']").change(function () { 
    var name = $(this).attr("name") + ",";
    if ($(this).prop("checked"))
        checkedCheckbox += name;
    else
        checkedCheckbox = checkedCheckbox.replace(name, "");   
    $("span").text(checkedCheckbox);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<input type="checkbox" name="A"/>
<input type="checkbox" name="B"/>
<input type="checkbox" name="C"/>
<input type="checkbox" name="D"/>
<input type="checkbox" name="E"/>
<input type="checkbox" name="F"/>
<span></span>

关于jquery - 删除未选中复选框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129382/

相关文章:

javascript - jQuery - hasClass 问题

javascript - 查找 tr 中的复选框是否被选中

css - 如何在 Bootstrap 中拥有一个视觉上活跃(选中)、禁用的切换​​按钮?

C# ListView with CheckBoxes,多选行时自动选中复选框

C# 跳过复选框 ListView

javascript - 如何使用搜索查询表单?

javascript - 使用FabricJS+React+Jquery无法点击添加文本

jquery - 如何使用 jQuery 获取多文件输入的更改值?

forms - 对一组复选框使用 HTML5 "required"属性?

javascript - 将从 localstorage 检索到的值插入到输入字段中