gpt4 book ai didi

javascript - 复选框始终给出最后一个值

转载 作者:行者123 更新时间:2023-12-03 12:32:47 25 4
gpt4 key购买 nike

我正在尝试从您选择的框中获取名字和姓氏。问题是它只给我最后一个值,你选择哪个框并不重要。

for (var i = 0; i<obj.length; i++) {
firstname = obj[i].Firstname;
lastname = obj[i].Lastname;

var checkbox = document.createElement('input');

checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
checkbox.id = "id";

var divElement = document.createElement("div");

divElement.id = "myDiv";
divElement.className = "myDivClass";
divElement.style.border = "1px solid black";

var userPar= document.createElement("p");

userPar.innerHTML = firstname + " " + lastname;
document.getElementById("users").appendChild(divElement);
divElement.appendChild(userPar);
divElement.appendChild(checkbox);
}

function save() {
$('input[type=checkbox]').each(function () {
if (this.checked) {
var first = firstname;
var last = lastname;
console.log(firstname);
console.log(lastname);
}
});
}

最佳答案

试试这个,

for (var i = 0; i<obj.length; i++) {
firstname = obj[i].Firstname;
lastname = obj[i].Lastname;

var checkbox = document.createElement('input');

checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
checkbox.id = "id" + i;

var divElement = document.createElement("div");

divElement.id = "myDiv" + i;
divElement.className = "myDivClass";
divElement.style.border = "1px solid black";

var userPar= document.createElement("p");

userPar.innerHTML = firstname + " " + lastname;
document.getElementById("users").appendChild(divElement);
divElement.appendChild(userPar);
divElement.appendChild(checkbox);
}

function save() {
$('input[type=checkbox]').each(function (index, val) {
if ($(val).is("checked")) {

var RawText = $(val).parent().find("p").text();
var first = RawText.split(" ")[0];
var last = RawText.split(" ")[1];
console.log(firstname);
console.log(lastname);
}
});
}

关于javascript - 复选框始终给出最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23855278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com