简体   繁体   中英

How to get all elements with class name in list form in javascript

I have multiple email inputs:

<input type="text" class="email" value="{{email}}">

And I want to retrieve each email input and put them all in one list:

var names = $(".email").val();

But right now I only get the first value instead of the full list. What's wrong?

您可以使用$.map创建值的数组。

var names = $.map($(".email"), function(e) {return e.value});

jQuery.each function will help you,

var email array = [];

jQuery(".email").each(function{
  email_array[email_array.length] = jQuery(this).val();
  #you can any operation in this block
});

now, you have all email in email_array

I'm guessing you're looking for a string list?

Try the "each" function!

var names = "";
$(".email").each(function(){
    if(names != ""){
       names += ", ";
    }
    names += $(this).val();
});

console.log(names);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM