获取输入的长度、id和名称;标签

Getting length, id and name of "input" tags

本文关键字:标签 id 输入 获取      更新时间:2023-09-26

如何获得div内所有input标签的长度,id和名称只有类名?

HTML:

<div id="division1" class="clonedInput kudd">
    <div class="set1">
        <label for="eee">Name</label>
        <input id="ee2" type="text" value="" name="ee2">
        <input id="ee3" type="text" value="" name="ee3">
    </div>
jQuery:

$('.set1 > input').attr('id').length;
$('.set1 > input').attr('id'); //No output

您可以使用each:

$('.set1 input').each(function() {
    var $this = $(this); // Caching
    var id = $this.attr('id');
    var name = $this.attr('name');
    var length = $this.val().length;
    console.log('ID: ' + id + '    Name: ' + name + '    Length: ' + length);
});

泛型迭代器函数,可用于对对象和数组进行无缝迭代。具有长度属性的数组和类数组对象(例如函数的arguments对象)通过数字索引进行迭代,从0到length-1。其他对象通过其命名属性进行迭代。

文档:http://api.jquery.com/jQuery.each/

$('.set1 input').each(function() {
var length= $(this).val().length;
    var id = $(this).attr('id');
 var name= $(this).attr('name');
});
$('.set1 input').each(function(){
    console.log($(this).attr('id'));
});

使用.map()在jquery

var map = $('.set1 input').map(function() {
    return { 'id' : $(this).attr('id'),
             'name' :  $(this).attr('name')
           }
}); 
console.log(map)

使用jquery $.each()函数获取所有输入记录

演示

泛型迭代器函数,可用于无缝迭代对象和数组。数组和类数组对象长度属性(例如函数的参数对象)被迭代通过数字索引,从0到长度-1。其他对象通过迭代

JQUERY

 $(document).ready(function(){
    $('.set1 > input').each(function(){
     console.log($(this).attr('id') +"=="+$(this).attr('id').length);
    });
 });
相关文章: