jquery从特定的表单获取所有输入

jquery从特定的表单获取所有输入

本文介绍了jquery从特定的表单获取所有输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法来填充来自某种形式的所有输入?
让我们说一些这样的事情:

 < form id =unique00> 
< input type =textname =whateverid =whatevervalue =whatever/>
< div>
< input type =checkboxname =whateverid =whatevervalue =whatever/>
< / div>
< table>< tr>< td>
< input type =hiddenname =whateverid =whatevervalue =whatever/>
< input type =submitvalue =qweqsac/>
< / td>< / tr>< / table>
< / form>
< form id =unique01>
< div>
< input type =textname =whateverid =whatevervalue =whatever/>
< input type =checkboxname =whateverid =whatevervalue =whatever/>
< / div>
< table>< tr>< td>
< input type =hiddenname =whateverid =whatevervalue =whatever/>
< / td>< / tr>< / table>
< select> blah ...< / select>
< input type =submitvalue =qweqsac/>
< / form>
等形式...形式...

*注意:每个表单可能有一个不同数量的输入和类型以及不同的html结构

那么是否有任何方式从某些表单ID填充输入?例如,如果我点击从某个表单ID提交按钮,然后jquery将填充这些表单ID中的所有输入。
目前我正在做的是这样的:

$ $ $ $ $ $ $form)。submit(function() {return validateForm($(this))});
函数validateForm(表单){
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){
casename:
and other case ...
}
})
}

这是工作,
但在这种情况下,我只得到field.name和field.value,实际上我想要的是,我想为每个输入元素提供一个jquery对象,这样我就可以访问它们的css,id,name,甚至可以为那些输入元素设置动画效果。



有没有办法解决这个问题?

请让我知道,并提前致谢!
AnD

解决方案

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

  $(form#formID:input)。each(function(){
var input = $(this); // This是输入的jquery对象,做什么你会
});

这使用jquery

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

等。


Is there any ways to populate all of the input from certain form?let say, some thing like this:

<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...

*note: each form might have a different amount of input and type and also different html structure

so is there any way to populate the input from certain form id? eg if i click submit button from certain form id, then jquery will populate for me all of the input within those form id.currently what i'm doing is like this:

$("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...
      }
    })
 }

that was work,but in that case, i only get the field.name and field.value, and actually what i want is, i want a jquery object for each input element, so i can access their css, id, name, and even animate those input element

is there any way for this?

please let me know and thank you in advance!AnD

解决方案

To iterate through all the inputs in a form you can do this:

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

This uses the jquery :input selector to get ALL types of inputs, if you just want text you can do :

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

etc.

这篇关于jquery从特定的表单获取所有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:54