我正在尝试将一些带有键的值推送到数组中,并且我想使用键获取详细信息以在警报消息中显示主题;
这是我尝试过
Jsfiddle
Html的代码
<table>
<tr class='itemIndex'>
<td class="id">18445</td>
<td class="firstName">Kakali</td>
<td class="occupation">Poultry farm</td>
<td class="sector">Business</td>
<td class="userInvestment">200</td>
</tr>
<tr class='itemIndex'>
<td class="id">18560</td>
<td class="firstName">Jamuna</td>
<td class="occupation">Handloom Work</td>
<td class="sector">Business</td>
<td class="userInvestment">300</td>
</tr>
<tr class='itemIndex'>
<td class="id">18562</td>
<td class="firstName">Champa</td>
<td class="occupation">Paddy processing</td>
<td class="sector">Business</td>
<td class="userInvestment">200</td>
</tr>
</table>
脚本
$(document).ready(function(){
var cartItem = [{id:''},{name:''},{occpation:''},{sector:''},{amount:''}];
$('.itemIndex').each(function(){
var element = $(this);
var itemId= element.children('.id').text();
var firstName = element.children('.firstName').html();
var occupation = element.children('.occupation').html();
var sector = element.children('.sector').html();
var userInvestment = element.children('.userInvestment').html();
cartItem.push[{'id':+itemId},{'name':+firstName},{'occpation':+occupation},{'sector':+sector},{'amount':+userInvestment}];
}).promise().done(function(){
for(var i=0;i<cartItem.length;i++)
{ alert(cartItem.id[i],cartItem.name[i],cartItem.occupation[i],cartItem.sector[i],cartItem.amount[i]);
}
});
});