无法使用jQuery使用数组中的键来推送值

时间:2013-08-07 04:40:41

标签: javascript jquery


我试图用键将一些值推送到数组,我想用键来获取详细信息以在警报消息中显示主题;
这是我试过的代码 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]);
                } 
            });    
});

3 个答案:

答案 0 :(得分:3)

你是说像这样的东西吗?

$(document).ready(function(){  
var cartItem = [];   
            $('.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();
                //alert(itemId);
                cartItem.push({'id':itemId,'name':firstName,'occupation':occupation,'sector':sector,'amount':userInvestment});
                //console.log( cartItem );
            }).promise().done(function(){
                 console.log( cartItem );
                for(var i=0;i<cartItem.length;i++) {
                    console.log(cartItem[i].id,cartItem[i].name,cartItem[i].occupation,cartItem[i].sector,cartItem[i].amount);
                } 
            });    
});

更新了Fiddle

答案 1 :(得分:0)

我不确定你到底在做什么,但请考虑这段代码(小提琴http://jsfiddle.net/vittore/vmszk/7/

$(document).ready(function(){

var cart = []

$('.itemIndex').each(function() {
    var item = {}

    $(this).children().each(function() {
       item[this.className] = this.innerHTML;  
    })
    cart.push(item)        
})


$('pre').text(JSON.stringify(cart))


});

答案 2 :(得分:0)

由于您不熟悉Javascript,因此存在大量代码错误。我已经修复了你的错误并且它正在运行。

建议您学习使用console.log而不是alert

$(document).ready(function() {   var cartItem = [];   $('.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
    });   })   for ( var i = 0; i < cartItem.length; i++) {
    alert(JSON.stringify(cartItem[i]));   } });

工作小提琴:http://jsfiddle.net/vmszk/9/