JS。使用id获取多个值

问题描述 投票:-2回答:1
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的值。

javascript html selector computer-science id
1个回答
0
投票

我认为它们之间存在一些问题,它们之间有空格/等等。

我找到了解决问题的解决办法。

刚用过:

var name = "a" + target;
var nameVal = document.getElementById(name).value;

等等。

© www.soinside.com 2019 - 2024. All rights reserved.