在 JQuery 中获取具有名称、ID、值的所有输入类型

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

Get all input types with name, id, value in JQuery

jquery

提问by Pradip

In JQuery, how can i get list of input type with name, id and value present in any div?

在 JQuery 中,如何获取任何 div 中存在的名称、ID 和值的输入类型列表?

回答by HandiworkNYC.com

Selects inputs that descend from a div that have both name, id, and value attributes present. Then pushes the "type" attribute of each matching item into an array.

选择从具有名称、ID 和值属性的 div 下降的输入。然后将每个匹配项的“type”属性推入一个数组。

var inputTypes = [];

$('div input[name][id][value]').each(function(){
     inputTypes.push($(this).attr('type'));
});

http://api.jquery.com/multiple-attribute-selector/

http://api.jquery.com/multiple-attribute-selector/

回答by Andreas Wong

In JQuery, how can i get list of input type...

在 JQuery 中,如何获取输入类型列表...

This?

这个?

var inputTypes = [];
$('input[name!=""][value!=""][id!=""]').each(function() {
    inputTypes.push($(this).prop('type'));
});

The property!=""is necessary than just [property], this is to filter out things like

property!=""不仅仅是必要的[property],这是过滤掉诸如

<input type="text" name="something" value="" id="someId" />

<input type="text" name="something" value="" id="someId" />

(Which I assume you don't want to get input with any property equals to empty string)

(我假设您不想获得任何属性等于空字符串的输入)

回答by Paul Aldred-Bann

You can look at each input, get their attributes and check against an empty string for each. You could use any method you want to present them, here I'm just adding them to an array.

您可以查看每个输入,获取它们的属性并检查每个输入的空字符串。你可以使用任何你想展示它们的方法,在这里我只是将它们添加到一个数组中。

var inputs = new Array();  

$("input").each(function() {
    var name = $(this).attr("name");
    var id = $(this).attr("id");
    var val = $(this).val();

    if ((name) && name !== "") && ((id) && id !== "") && ((val) && val !== "")) {
        inputs.push(this);
    }
});

回答by Mateusz Rogulski

You can get access for input by:

您可以通过以下方式获得输入权限:

$('input')

then get attributes which you want by for example .attr('type'), .attr('id'). And if you need list write them into array is $('input').each(...);

然后通过例如.attr('type'),获取您想要的属性.attr('id')。如果你需要列表将它们写入数组是$('input').each(...);

回答by AmiNadimi

When You want to select an input with an specific name like "foo"do it as below :

当您想选择具有特定名称的输入时,"foo"请执行以下操作:

$('div input[name="foo"]').each(function(){
     // your program logic goes here
     // this.val("123")
});

Or you might wanna get ones which their value does not match "123":

或者你可能想得到那些它们的价值不匹配的"123"

$('div input[value!="123"]').each(function(){
     // your program logic goes here
});

Or even a combination of both :

或者甚至是两者的结合:

$('div input[name="foo"][value!="123"]').each(function(){
    // your program logic goes here
});

See JQuery API Referencefor more details.

有关更多详细信息,请参阅JQuery API 参考