简体   繁体   English

如何从html div数组中获取特定值

[英]How to get specific value from html div array

My application calls a web api which returns an array.我的应用程序调用一个返回数组的 web api。 I am binding that to html div.我将其绑定到 html div。 This div contains checkbox, name, age.此 div 包含复选框、名称、年龄。

My requirement is to get values of name and age for the selected.我的要求是获取所选对象的姓名和年龄值。

I want to achieve this without using tables我想在不使用表格的情况下实现这一目标

var parentDiv = document.getElementById('accountsList');

var htmlData = "";
for (var i = 0; i < result.Accounts.length; i++) {
  htmlData += '<div class="row" id="accountRow">';
  htmlData += '<div class="col-sm-4"> <input type="checkbox" onclick="calculateTotal(this)"><span>Select</span></div>';
  htmlData += '<div class="col-sm-4"> Name ' + result.Accounts[i].Name +  '</div>';
  htmlData += '<div class="col-sm-4"> Age' + result.Accounts[i].Age+  '</div>';
  htmlData += '</div><hr>'; 
}
parentDiv.innerHTML += htmlData;

My requirement is to get values of name and age for the selected.我的要求是获取所选对象的姓名和年龄值。

If the information that you need is this simple, I would then use data-* attributes.如果您需要的信息如此简单,那么我将使用data-*属性。 If it is more complex, the maybe find would be a better solution.如果它更复杂,也许find将是更好的解决方案。

Since your data is so simple I used the data-* approach.由于您的数据非常简单,我使用了data-*方法。 Basically we save the data to the root element, then when we click on the checkbox we check it's parent for the data.基本上我们将数据保存到根元素,然后当我们单击复选框时,我们检查它的父级数据。 We could have placed the data on the checkbox if we would have rather done that.如果我们宁愿这样做,我们可以将数据放在复选框上。

Next we use getAttribute(key) to the get attribute on the element.接下来我们使用getAttribute(key)来获取元素上的属性。

 var parentDiv = document.getElementById('accountsList'); const result = { Accounts: [ {Name: 'Billy', Age: 10}, {Name: 'Bob', Age: 20}, {Name: 'Joe', Age: 30}, ] } var htmlData = ""; for (var i = 0; i < result.Accounts.length; i++) { htmlData += `<div class="row" id="accountRow_${i}" data-name="${result.Accounts[i].Name}" data-age="${result.Accounts[i].Age}">`; htmlData += '<div class="col-sm-4"> <input type="checkbox" onclick="calculateTotal(this)"><span>Select</span></div>'; htmlData += '<div class="col-sm-4"> Name: ' + result.Accounts[i].Name + '</div>'; htmlData += '<div class="col-sm-4"> Age: ' + result.Accounts[i].Age + '</div>'; htmlData += '</div><hr>'; } parentDiv.innerHTML += htmlData; function calculateTotal() { console.clear(); [...document.querySelectorAll('[type=checkbox]:checked')].forEach(itm => { let row = itm.closest('.row') console.log('Name:', row.getAttribute('data-name'), 'Age:', row.getAttribute('data-age')) }) }
 <div id="accountsList"></div>

One approach could be to use the index i to set the ids and then get the values of those ids.一种方法是使用索引 i 来设置 id,然后获取这些 id 的值。

For example例如

var htmlData = "";
for (var i = 0; i < result.Accounts.length; i++) {
  htmlData += '<div class="row" id="accountRow">';
  htmlData += '<div class="col-sm-4"> <input type="checkbox" onclick="calculateTotal(this, ' + i + ')"><span>Select</span></div>';
  htmlData += '<div class="col-sm-4" id="name' + i + '"> Name ' + result.Accounts[i].Name +  '</div>';
  htmlData += '<div class="col-sm-4"  id="age' + i + '"> Age' + result.Accounts[i].Age+  '</div>';
  htmlData += '</div><hr>'; 
}

Then, when clicking on the checkbox, trigger to which i it belongs.然后,当单击复选框时,触发它所属的i

var calculateTotal = (element, indexId) => {

    var name = $('#name' + indexId).value();
    var age = $('#age' + indexId).value();

... do your stuff here ...
}

Hope it helps!希望能帮助到你!

 var parentDiv = document.getElementById('accountsList'); const result = { Accounts: [ {Name: 'Billy', Age: 10}, {Name: 'Bob', Age: 20}, {Name: 'Joe', Age: 30}, ] } var htmlData = ""; for (var i = 0; i < result.Accounts.length; i++) { htmlData += `<div class="row" id="accountRow_${i}" data-name="${result.Accounts[i].Name}" data-age="${result.Accounts[i].Age}">`; htmlData += '<div class="col-sm-4"> <input type="checkbox" onclick="calculateTotal(this)"><span>Select</span></div>'; htmlData += '<div class="col-sm-4"> Name: ' + result.Accounts[i].Name + '</div>'; htmlData += '<div class="col-sm-4"> Age: ' + result.Accounts[i].Age + '</div>'; htmlData += '</div><hr>'; } parentDiv.innerHTML += htmlData; function calculateTotal() { console.clear(); [...document.querySelectorAll('[type=checkbox]:checked')].forEach(itm => { let row = itm.closest('.row') console.log('Name:', row.getAttribute('data-name'), 'Age:', row.getAttribute('data-age')) }) }
 <div id="accountsList"></div>

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

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