猿问

我可以通过使用该对象中的另一个值来获取数组中对象内部的特定值吗?

我有多个数组,其中包含三个键/值对,一个名称,一个电话号码和首字母缩写。我有一个循环,它创建一个复选框列表并将初始值设置为它的类。我试图弄清楚如何在选中该框时从相应的对象中提取电话号码值并将其保存到单独的数组中。例如:如果选中了类“.BRJ”的框,则将该对象中的 1222222222 数字保存到新数组中。


我知道我可以在输入中运行一个 onclick 函数,但我一生都无法弄清楚如何找到与该复选框匹配的电话号码。


const management = [

  { name: 'Bob Jones', number: 12222222222, initials: 'BRJ',},

  { name: 'Peter Jones', number: 1333333333, initials: 'PRJ',},   

];


const operatorLoop = (shift, list) => {


  for (var i = 0; i < shift.length; i++) {

    list.innerHTML += `

    <li><input type = 'checkbox' class ='${shift[i].initials}' 

    >${shift[i].name}</input></li>

  `}

}


operatorLoop(management, mgmtUl);


三国纷争
浏览 128回答 2
2回答

慕码人2483693

首先,您应该添加更多属性,其值为电话const operatorLoop = (shift, list) => {&nbsp; &nbsp;for (var i = 0; i < shift.length; i++) {&nbsp; &nbsp; &nbsp; list.innerHTML += `&nbsp; &nbsp; &nbsp; &nbsp;<li><input type = 'checkbox' data-number='${shift[i].number}' class ='${shift[i].initials}'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;>${shift[i].name}</input></li>`&nbsp; &nbsp;}}然后你检查所有复选框var sList = "";$('input[type=checkbox]').each(function () {&nbsp; &nbsp; sList += (this.checked ? "," + $(this).data('number') : "");});console.log (sList); //result that you expect: 12222222222, 1333333333
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答