jQuery jquery如何获取表单元素的类型、名称和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5666445/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:35:42  来源:igfitidea点击:

jquery how to get form element types, names and values

jqueryformselements

提问by PruitIgoe

I know I can get the name/value relationship by using

我知道我可以通过使用获得名称/值关系

$(#form).serializeArray();

But is there a way to get the whole enchilada, type, name and value with one call?

但是有没有办法通过一次调用获得整个辣酱玉米饼馅、类型、名称和值?

回答by Mark Coleman

Use $("form :input")

$("form :input")

Per the docs:

根据文档

Description: Selects all input, textarea, select and button elements.

描述:选择所有输入、文本区域、选择和按钮元素。

Now to your question,

现在回答你的问题,

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...
});

But if you want to return some sort of structure, you can use .map()

但是如果你想返回某种结构,你可以使用 .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


jsfiddle 上的示例

回答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 "fieldset",

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

回答by Josh

Can you loop through each inputelement of the form and use the data you get from there? Something like this:

您可以遍历input表单的每个元素并使用从那里获得的数据吗?像这样的东西:

$('form input').each(function(i, v) {
    // Access like this:
    //   $(this).attr('type');
    //   $(this).attr('value');
    //   $(this).attr('name');
});

回答by Bot

To get ALL form elements use

要获取所有表单元素,请使用

$('input, textarea, select').each(function() {
    //   $(this).attr('type');
    //   $(this).attr('name');
    //   $(this).val();
});

回答by no.good.at.coding

Perhaps children()will be more useful but you'll still have to filter elements of interest yourself, unless you use a selector.

也许children()会更有用,但您仍然必须自己过滤感兴趣的元素,除非您使用选择器。

You might also do the following if you only want to select successfulelements in the form; this will NOT include buttons or disabled fields.

如果您只想选择successful表单中的元素,您也可以执行以下操作;这将不包括按钮或禁用字段。

$($('#testf').serializeArray()).each(function(index, value){
    $('#testf [name="' + value.name + '"]'); //select the named element
});

Mark's answer seems like the best way, IMO.

马克的回答似乎是最好的方法,IMO。