简体   繁体   English

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>)});}

^ this code above here pulls data from the db fine. ^ 上面的这段代码从数据库中提取数据。

However, to save.... I need to get each values.但是,为了保存......我需要获取每个值。

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)
})}

I've tried many ways... Not sure how to get values for each ids using the target.我尝试了很多方法......不确定如何使用目标获取每个 ID 的值。

I think it had some issues with the ids having spaces/, etc. between them or something.我认为 ID 之间有空格 / 等存在一些问题。

I've found a work-around to solve the issue.我找到了一种解决方法来解决这个问题。

Just used:刚用过:

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

and etc.等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM