jQuery如何获取表单元素类型,名称和值 [英] jquery how to get form element types, names and values
本文介绍了jQuery如何获取表单元素类型,名称和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以通过使用
$(#form).serializeArray();
但是有一种方法可以通过一次调用获得整个辣酱玉米饼馅,类型,名称和值吗?
But is there a way to get the whole enchilada, type, name and value with one call?
推荐答案
使用$("form :input")
根据文档:
说明:选择所有输入, textarea,选择和按钮元素.
Description: Selects all input, textarea, select and button elements.
现在您的问题,
有一种方法可以得到全部 辣酱玉米饼馅,类型,名称和值与 一个电话?
there a way to get the whole enchilada, type, name and value with one call?
如果您只想遍历所有项目,
If you simply want to loop through the items,
$("form :input").each(function(index, elm){
//Do something amazing...
});
但是,如果您想返回某种结构,则可以使用.map()
But if you want to return some sort of structure, you can use .map()
var items = $("form :input").map(function(index, elm) {
return {name: elm.name, type:elm.type, value: $(elm).val()};
});
或者如果您只是想获取元素
Or if you simply want to get the elements
$("form :input").get()
Example of this on jsfiddle
这篇关于jQuery如何获取表单元素类型,名称和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文