js。使用 id 获取多个值
js. get multiple values using id
function handleUser(activeMenu, user) {
$('#accordion').html("");
$.each(user, (index, user) => {
$('#accordion').append(
<input class="control" id="a${user.userId}" value=${user.name}></input>
<input class="control" id="b${user.userId}" value=${user.email}></input>
<input class="control" id="c${user.userId}" value=${user.phone}></input>
<input class="control" id="d${user.userId}" value=${user.address}></input>
<button class="info-actions" id="${user.userId}">
save
</button>)});}
^ 上面的这段代码可以很好地从数据库中提取数据。
但是,要保存....我需要获取每个值。
function handleInfo() {
$('#accordion').on('click','.info-actions',(event) => {
let target = $(event.target).attr('id');
console.log(target);
//this prints the id fine.
//let x = $('.control').attr('value');
//let x = $(`#a${target}`);
//let x = $(`#a${target}`);
//let x = $('#a${target}').val();
console.log(x);
//..... then going to use ajax to connect controller.(Which I can handle)
})}
我试过很多方法...
不确定如何使用目标获取每个 ID 的值。
我认为它有一些问题,id 之间有空格/等等。
我找到了解决问题的方法。
刚用过:
var name = "a" + target;
var nameVal = document.getElementById(name).value;
等等
function handleUser(activeMenu, user) {
$('#accordion').html("");
$.each(user, (index, user) => {
$('#accordion').append(
<input class="control" id="a${user.userId}" value=${user.name}></input>
<input class="control" id="b${user.userId}" value=${user.email}></input>
<input class="control" id="c${user.userId}" value=${user.phone}></input>
<input class="control" id="d${user.userId}" value=${user.address}></input>
<button class="info-actions" id="${user.userId}">
save
</button>)});}
^ 上面的这段代码可以很好地从数据库中提取数据。
但是,要保存....我需要获取每个值。
function handleInfo() {
$('#accordion').on('click','.info-actions',(event) => {
let target = $(event.target).attr('id');
console.log(target);
//this prints the id fine.
//let x = $('.control').attr('value');
//let x = $(`#a${target}`);
//let x = $(`#a${target}`);
//let x = $('#a${target}').val();
console.log(x);
//..... then going to use ajax to connect controller.(Which I can handle)
})}
我试过很多方法... 不确定如何使用目标获取每个 ID 的值。
我认为它有一些问题,id 之间有空格/等等。
我找到了解决问题的方法。
刚用过:
var name = "a" + target;
var nameVal = document.getElementById(name).value;
等等