Javascript jquery 从特定表单获取所有输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4291005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery get all input from specific form
提问by AnD
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
*注意:每个表单可能有不同数量的输入和类型以及不同的 html 结构
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:
那么有没有办法从某个表单 id 填充输入?例如,如果我从某个表单 id 单击提交按钮,那么 jquery 将为我填充这些表单 id 中的所有输入。目前我正在做的是这样的:
$("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
那是可行的,但在那种情况下,我只得到 field.name 和 field.value,实际上我想要的是,我想要每个输入元素的 jquery 对象,这样我就可以访问它们的 css、id、name 和甚至动画那些输入元素
is there any way for this?
有什么办法吗?
please let me know and thank you in advance! AnD
请让我知道并提前感谢您!和
回答by TJB
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 selectorto get ALL types of inputs, if you just want text you can do :
这使用 jquery :input 选择器来获取所有类型的输入,如果你只想要文本,你可以这样做:
$("form#formID input[type=text]")//...
etc.
等等。
回答by Srinivasan.S
The below code helps to get the details of elements from the specific form with the form id,
下面的代码有助于从具有表单 id 的特定表单中获取元素的详细信息,
$('#formId input, #formId select').each(
function(index){
var input = $(this);
alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
}
);
The below code helps to get the details of elements from all the forms which are place in the loading page,
下面的代码有助于从加载页面中的所有表单中获取元素的详细信息,
$('form input, form select').each(
function(index){
var input = $(this);
alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
}
);
The below code helps to get the details of elements which are place in the loading page even when the element is not place inside the tag,
下面的代码有助于获取放置在加载页面中的元素的详细信息,即使元素未放置在标签内,
$('input, select').each(
function(index){
var input = $(this);
alert('Type: ' + input.attr('type') + 'Name: ' + input.attr('name') + 'Value: ' + input.val());
}
);
NOTE:We add the more element tag name what we need in the object list like as below,
注意:我们在对象列表中添加我们需要的更多元素标签名称,如下所示,
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());
}
);
回答by Markoj
Use HTMLForm "elements" attribute:
使用HTML表单的“元素”属性:
$.each($("form").elements, function(){
console.log($(this));
});
Now it's not necessary to provide such names as "input, textarea, select ..." etc.
现在没有必要提供诸如“input、textarea、select ...”等名称。