开发者

jQuery .each through POST nested array

开发者 https://www.devze.com 2023-04-05 06:28 出处:网络
I have a POST that looks something like this: Array ( [students] => Array ( [37] => Array ( [name] => 37

I have a POST that looks something like this:

Array
(
[students] => Array
    (
        [37] => Array
            (
                [name] => 37
                [registration_payment_date] => 
                [check_amount] => 120.00
            )

        [38] => Array
            (
                [name] => 38
                [registration_payment_date] => 9-11-2011
                [check_amount] => 117.00 

            )
     )
)

I want to loop through the key and value of the inner array in the .submit function to verify that if there is a check amount then there should be a payment date. If no payment date then I will add that name to an array then alert that list of names. I'm having trouble with the jQuery.each function. I'm assuming there should be an outer .each and an inner .each. I have var form = this then-

var names = "";
jQuery(form.students).each(function(){
    //ANOTHER EACH HERE?? {
        //AN IF STATEMENT HERE {
            names.push开发者_StackOverflow( students['name'];
        }
    })
});
alert(names);

I'm close, I think, but not quite there. Can anyone fill in the blanks (or tell me I'm off track) please?


You pretty much have it, when you use jQuery each, it gives you an index of the current item you are looping over. we use i in this case.

var names = "";
$(form.students).each(function(i,el){
    $(form.students[i].each, function() {
        //AN IF STATEMENT HERE {
            names.push( students[i]['name'];
        }
    })
});
alert(names);


var names=new Array();
$.each('form.students',function(){
    $.each(this,function() {
            names.push(this.name);
        });
    });
alert(names);

I removed if because i cant get what u want clearly so u can put yourself accordingly.
Is this what ur looking for.

0

精彩评论

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