javascript - 如何使用 javascript 或 jquery 通过选中的复选框获取用户名?

标签 javascript jquery checkbox

enter image description here

HTML

  <div id="notApprovedUsers">

                            </div><button onclick="approveUsers()">Approve</button>
                            </div>

我喜欢在参数 userList 中获取已检查的用户来使用。

这是 javascript,我生成包含用户和复选框的列表。 我当然喜欢只批准已检查的!

JavaScript

function getNotAssignedUsers() {

server.getUsersByGroup("1eb33e30-c355-467f-af3c-e7d0b4a1fgt5").then(function (userDetails) {
    debugger;

    for (var i = 0; i < userDetails.length; i++)
    {
        var vorname = userDetails[i].firstName;
        var nachname = userDetails[i].lastName;

        var newCheckBox = document.createElement('input');
        newCheckBox.type = 'checkbox';
        newCheckBox.id = "0"+i;
        document.getElementById("notApprovedUsers").appendChild(newCheckBox);


        var newElement = document.createElement('div');
        newElement.id = i;
        newElement.className = "notApprovedUsers";
        newElement.innerHTML = vorname + " " + nachname;
        document.getElementById("notApprovedUsers").appendChild(newElement);
    }                                         
});

}

此处在 userList 中我需要读取已选中的用户..

function approveUsers(userList)
{
    var user1 = document.getElementById("00").checked;
    var user2 = document.getElementById("01").checked;
    var user3 = document.getElementById("02").checked;

    alert(user1 || user2 || user3);
}

最佳答案

那么你想要这样的东西。由于我没有测试过,可能会有错误

var names = [];

$('input[type=checkbox]:checked').each(function(index, value){

    var name = $(value).next("div").html();
    names.push(name);
});

然后对名称数组执行一些操作

编辑 - 请参阅 jsfiddle http://jsfiddle.net/vpg3r/3/

关于javascript - 如何使用 javascript 或 jquery 通过选中的复选框获取用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19956469/

相关文章:

javascript - 如何在 javascript 或 Jquery 中调用 SOAP (XML) Web 服务?

javascript - 使用 Enter Keydown Event 移动到下一个输入

javascript - 同时在标签和复选框上绑定(bind) JavaScript jQuery 单击事件

javascript - Angular JS : Filter using textbox and Checkbox

javascript - 基于复选框或切换选择 (Tableau API) 在 Javascript 函数之间切换

javascript - Kendo Grid 复选框禁用编辑

javascript - jQuery:删除浏览器生成的标题属性

javascript - 当 if 条件包含或 (||) 两个变量时,jQuery if/else 语句

javascript - 如何在动画/过渡中间暂停

javascript - 添加值以动态选择字段