How to loop through elements of forms with JavaScript?

后端 未结 7 1298
無奈伤痛
無奈伤痛 2020-12-05 01:53

I have a form:

7条回答
  •  春和景丽
    2020-12-05 02:33

    Es5 forEach:

    Array.prototype.forEach.call(form.elements, function (inpt) {
           if(inpt.name === name) {
                 inpt.parentNode.removeChild(inpt);
           }
    });
    

    Otherwise the lovely for:

    var input;
    for(var i = 0; i < form.elements.length; i++) {
         input = form.elements[i];
    
          // ok my nice work with input, also you have the index with i (in foreach too you can get the index as second parameter (foreach is a wrapper around for, that offer a function to be called at each iteration.
    }
    

提交回复
热议问题