jquery form elements to array
I'm trying to get all form elements to array, then loop through them using jquery's $.each() function and within this function get each element's id, and title attributes.
I've tried serializeArray(), but I can only get 'name' and 'value' attributes.
I need something that collects all form elements whether it's input (regardless of the type), select, textarea, and regardless whether it has value, is checked or hidden.
Perhaps something like $('#form_id').find('input select textarea'); ?
Any idea how to achieve this?
Use:
$('#form_id')[0].elements
It will return a nodeList containing all form-elements.
jQuery(
  function($)
  {
    $.each($('#form_id')[0].elements,
           function(i,o)
           {
            var _this=$(o);
            alert('id:'+_this.attr('id')+'ntitle:'+_this.attr('title'));
           })
  }
);
 <edit>  
 Please Note: the elements-collecttion may also contain elements like fieldset or object, See: http://www.w3.org/TR/html5/forms.html#category-listed  
 </edit>  
您可以使用:input伪类获取所有表单元素: 
var elems = $('#form_id').find(':input'); // changed this line
我认为这可以按照你想要的方式工作:
$('#form_id').children('input, select, textarea')
上一篇: wymeditor设定值?
下一篇: jQuery的表单元素数组
