javascript - js。使用 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;
等等。
推荐阅读
- google-app-maker - 谷歌应用程序制造商:如何列出现有组?
- java - 如何确定列表中是否有提供总和的 2 个元素?
- python - 如何将 Lambda 层作为输入层添加到 Keras 中的现有模型中?
- python - 在 matplotlib 中使用数组制作滑块
- linux - X86_64 汇编代码段错误并给出堆栈粉碎错误
- java - 在 Kubernetes 中找不到客户端会话 ID
- java - Apk 未正确构建
- deployment - DigitalOcean - 在预编译资产上限制登台部署错误
- java - 在 SpringSecurity 中未调用自定义 UserDetailsService Impl
- sql - sqlopenresult' status='-239' / cognos 报错