创建jQuery的关联数组 [英] create an associative array in jquery

查看:1119
本文介绍了创建jQuery的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我迄今为止和的鞋类型的是靴,筒靴,皮革,培训人员(按顺序)

我要遍历并赋值所以我富人像

  VAR shoeArray = {靴款:3,统靴:'0',皮:1,培训师:3};

此刻我只想找到的数组{3,0,1,3} 我可以工作,但它是不是非常有帮助。

 函数shoe_types(){
    变种shoeArray = [];
    $('[类型=数字]')。每个(函数(){
        $('跨度[数据字段='+ $(本).attr('身份证')+']')文本($(本).VAL());
        shoeArray.push(parseInt函数($(本).VAL()));
    });
    返回shoeArray;
}


解决方案

勾选此功能

 函数shoe_types(){
    变种shoeArray = {}; //注意,这
    $('[类型=数字]')。每个(函数(){
       $('跨度[数据字段='+ $(本).attr('身份证')+']')文本($(本).VAL());
       shoeArray [$(本).attr('身份证')] = parseInt函数($(本).VAL());
    });
    返回shoeArray;}

PS:假设 $(本).attr('身份证')拥有所有类型的鞋

This is what I have so far and the shoe types are boots, wellingtons, leather, trainers (in that order)

I want to iterate through and assign the value so I haves something like

var shoeArray = { boots : '3', wellingtons: '0', leather : '1', trainers: '3'};

at the moment I just get an array of {3,0,1,3} which I can work with but it is not very helpful.

function shoe_types() {
    var shoeArray = [];
    $('[type=number]').each(function(){
        $('span[data-field='+$(this).attr('id')+']').text($(this).val());      
        shoeArray.push ( parseInt($(this).val()) );      
    });             
    return shoeArray;        
}

解决方案

Check this function

function shoe_types() {
    var shoeArray = {}; // note this
    $('[type=number]').each(function(){
       $('span[data-field='+$(this).attr('id')+']').text($(this).val());
       shoeArray[$(this).attr('id')] =  parseInt($(this).val()) ;
    });
    return shoeArray;

}

PS: Assuming $(this).attr('id') has all the shoe types

这篇关于创建jQuery的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆