jquery从特定的表单中获取所有input

有什么方法来填充从某种forms的所有input? 比方说,有些事情是这样的:

<form id="unique00"> <input type="text" name="whatever" id="whatever" value="whatever" /> <div> <input type="checkbox" name="whatever" id="whatever" value="whatever" /> </div> <table><tr><td> <input type="hidden" name="whatever" id="whatever" value="whatever" /> <input type="submit" value="qweqsac" /> </td></tr></table> </form> <form id="unique01"> <div> <input type="text" name="whatever" id="whatever" value="whatever" /> <input type="checkbox" name="whatever" id="whatever" value="whatever" /> </div> <table><tr><td> <input type="hidden" name="whatever" id="whatever" value="whatever" /> </td></tr></table> <select>blah...</select> <input type="submit" value="qweqsac" /> </form> etc forms... forms... 

*注意:每个表单可能有不同数量的input和types,也有不同的html结构

那么有什么办法来填充从某些表单IDinput? 例如,如果我点击提交button从某些表单ID,然后jQuery将填充我在表单ID的所有input。 目前我正在做的是这样的:

 $("form").submit(function(){ return validateForm($(this)) }); function validateForm(form){ var retVal = true; var re; $.each(form.serializeArray(), function(i, field) { var input = $('input[name='+field.name+']'); field.value = $.trim(field.value); switch(field.name){ case "name" : and another cases... } }) } 

这是工作,但在这种情况下,我只得到field.name和field.value,实际上我想要的是,我想为每个input元素的jquery对象,所以我可以访问他们的CSS,ID,名称和甚至为这些input元素添加animation

有什么办法吗?

请让我知道,并提前感谢您! 和

要遍历表单中的所有input,可以这样做:

 $("form#formID :input").each(function(){ var input = $(this); // This is the jquery object of the input, do what you will }); 

这使用jQuery的:inputselect器来获得所有types的input,如果你只是想要文本,你可以做:

 $("form#formID input[type=text]")//... 

等等

下面的代码有助于从表单id中获取具体表单元素的细节,

 $('#formId input, #formId select').each( function(index){ var input = $(this); alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val()); } ); 

下面的代码有助于从加载页面中的所有表单中获取元素的详细信息,

 $('form input, form select').each( function(index){ var input = $(this); alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val()); } ); 

下面的代码有助于获取放置在加载页面中的元素的细节,即使元素不在标签内,

 $('input, select').each( function(index){ var input = $(this); alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val()); } ); 

注意:我们在对象列表中添加更多元素标签名称,如下所示,

 Example: to get name of attribute "textarea", $('input, select, textarea').each( function(index){ var input = $(this); alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val()); } ); 

使用HTML表单“元素”属性:

$.each($("form").elements, function(){ console.log($(this)); }

现在没有必要提供“input,textarea,select …”等名称