How to loop through all elements of a form jQuery

前端 未结 9 2103
Happy的楠姐
Happy的楠姐 2020-12-14 00:11

I was just wondering what the best way of looping through all the child elements of a form would be? My form contains both input and select elements.

At the moment I

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 00:51

    pure JavaScript is not that difficult:

    for(var i=0; i < form.elements.length; i++){
        var e = form.elements[i];
        console.log(e.name+"="+e.value);
    }
    

    Note: because form.elements is a object for-in loop does not work as expected.

    Answer found here (by Chris Pietschmann), documented here (W3S).

提交回复
热议问题