开发者

How can i select an object contained within another. jQuery

I have a script that adds several of these forms.

var html = '<div class="equipment_row">' +
'<img src="'+base_url+'verkstad/icons/icon-'+type+'.png"/>' +
'<input class="eq_amount" type="text" value="'+amount+'" />' +
'<input class="eq_artnr" type="text" value="'+artnr+'" />' +
'<input class="eq_text" type="text" value="'+text+'" />' +
'<input class="eq_price" type="text" value="'+price+'" />' +
'<img src="'+base_url+'verkstad/icons/eq_delete.png" class="eq_delete_img"/>' +
</div>';
$('#equipment_container').append(html);

Then when the user hits "save" I want it to collect all these values in an array

var eq_array = Array();
var obj;
$('.equipment_row').each(function(){
obj = new Object();
obj.amount = ??? 
obj.artnr = ???
obj.text = ???
obj.price = ???
eq_array.push(obj);
});
var dataString = $.toJSON(eq_array);
alert(dataString);

How can I acces开发者_如何学编程s the containing input fields in "equipment_row"?


It would be a lot easier to wrap it all in a form element and do $(form).serialize();


$('.equipment_row').each(function(i, e){
obj = new Object();
obj.amount = $('.eq_amount', e).val();

and so on..


i'm surprised there is no names on your inputs, but only a class.

var eq_array = new Array(); // = [];
$('.equipment_row').each( function () {
    var obj = new Object(); // = {};
    $('input, select', this).each( function () {
        obj[$(this).attr('class')] = this.value;
    });
    eq_array.push(obj);
});

note with this your array will look like

[ { eq_amout: 2113,
    eq_artnr: ...
  },
  ...
]


use:

obj.amount = $(this).find('.eq_amount');

and stuff like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜