-2


我正在尝试将一些带有键的值推送到数组中,并且我想使用键获取详细信息以在警报消息中显示主题;
这是我尝试过
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]);
                } 
            });    
});
4

3 回答 3

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);
                } 
            });    
});

更新小提琴

于 2013-08-07T04:49:04.790 回答
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))


});
于 2013-08-07T04:52:48.183 回答
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/

于 2013-08-07T05:01:45.857 回答