javascript - foreach 只产生一个结果

标签 javascript jquery html

我有三个电子邮件文本框,其中包含三个不同的值。但它产生相同的结果。我有这个:

$('.contactpersonen-email').each(function() {
    var mails = $('.contactpersonen-email').map(function(){                 
        return $(this).val();   
    }).get(); 
    console.log(mails); 
});

和 HTML:

<div class="contact-label span2">
    <label for="contactpersonen-email">Email adres</label>
    <div class="contact-input-field">
        <input type="text" class="input-text span2  contactpersonen-email" id="contactpersonen-email0" name="contactpersonen-email"></input>
    </div>
</div>

这是整个脚本:

<![CDATA[
                            $(document).ready(function()
                            {
                                // Form validation
                                $(".klantregistratie form").validate({
                                    rules: {
                                        verploegen_form_klantregistratie_Bedrijfsnaam: "required",
                                        verploegen_form_klantregistratie_Postcode: "required",
                                        verploegen_form_klantregistratie_Plaats: "required",
                                        verploegen_form_klantregistratie_Emailadres: {
                                            required: true,
                                            email: true
                                        },
                                        verploegen_form_klantregistratie_KvK_nummer: "required",
                                        verploegen_form_klantregistratie_naam_eigenaar: "required",
                                        verploegen_form_klantregistratie_Telefoon_mobiel: "required"
                                    },
                                    messages: {
                                        verploegen_form_klantregistratie_Bedrijfsnaam: "De bedrijfsnaam is niet ingevuld",
                                        verploegen_form_klantregistratie_Postcode: "De postcode is niet ingevuld",
                                        verploegen_form_klantregistratie_Plaats: "De plaats is niet ingevuld",
                                        verploegen_form_klantregistratie_Emailadres: {
                                            required: "Je hebt geen email adres ingevuld",
                                            email: "Je hebt geen geldig email adres ingevuld"
                                        },
                                        verploegen_form_klantregistratie_KvK_nummer: "Het KVK nummer is niet ingevuld",
                                        verploegen_form_klantregistratie_naam_eigenaar: "De naam van de eigenaar is niet ingevuld",
                                        verploegen_form_klantregistratie_Telefoon_mobiel: "Het (mobiele) telefoon nummer is niet ingevuld"
                                    }
                                });




                                // Hide the textarea
                                $('#verploegen_form_klantregistratie_Contactpersonen').parent().parent().hide();

                                // Add extra contact clicked?
                                $('#add-contact p a').click(function()
                                {
                                    // Make a copy of the first input fields
                                    html = $('#new-contact').children().clone();

                        // Get number of tabs in the accordion
                        var index = $('#accordion h3').length;

                                    // Remove the values
                                    html.find("input[type=text]").val("");
                  html.find('input[type=checkbox]').attr('checked', false);

                        // New 'id', 'for' and 'name' attribute names
                        html.find('input[type=checkbox]').each(function () {
                            me = $(this);
                            attr = me.attr('id');
                            number = attr.split('_')[2];
                            newNumber = parseInt(index) + 1;
                            newAttr = attr.replace(number, newNumber);
                            me.attr('id', newAttr).attr('name', newAttr).next().attr('for', newAttr);
                        });                  

                                    // Insert it at the end
                        $('#accordion').append(html);
                        $('#accordion').accordion('refresh');

                        // Set last tab to active
                        $("#accordion").accordion({ active: index });

                                    // Cancel the click
                                    return false;
                                });

                                // Submit clicked?
                                $('.submit-button input').click(function()
                                {
                                    // Set variables
                                    var html = '';
                                    var i = 1;

                        // Loop through all the input fields for contacts
                        $('#accordion .user-row').each(function (uindex, uvalue) {


var mails = [];
$('.contactpersonen-email').each(function() {
    mails.push($(this).val());
});
console.log(mails);




        Array.prototype.areUnique = function() {
    var l = this.length;
    for(var i=0; i<l; i++) {
        for(var j=i+1; j<l; j++) {
            if (this[i] === this[j]) return false;
        }
    }
    return true;
};

//if (!mails.areUnique()) {
  // console.log ('values are not unique');
//} else {
 //   console.log('values are unique');
//}         


                            html += '<tr>';

                            $(this).find('input').each(function (index, value) {



                                // Check if input type is a checkbox
                                if ($(this).is(":checkbox")) {
                                    var JaNee = 'Nee';
                                    if ($(this).is(":checked")) JaNee = 'Ja';
                                    html = html + '<td>' + JaNee + '</td>';
                                }
                                else {
                                    // Add the value into the html
                                    html = html + '<td>' + $(this).val() + '</td>';
                                }
                            });

                            html += '</tr>';
                        });

                                    // Set the value from the textarea to the generated html
                                    $('#verploegen_form_klantregistratie_Contactpersonen').val('<table>'+html+'</table>');
                                });
                            });

              $(function () {
                    $("#accordion").accordion({
                        heightStyle: "content"
                    });
                });
                        ]]>

非常感谢。也有验证。和 Accordion 。 html 是一样的。所以有一个电子邮件文本框

我现在是这样的:

 // Loop through all the input fields for contacts
                        $('#accordion .user-row').each(function (uindex, uvalue) {


var mails = $('.contactpersonen-email').map(function(){                 
        return $(this).val();   
    }).get(); 
    console.log(mails);

最佳答案

您可能想尝试以下操作:

var mails = [];
$('.contactpersonen-email').each(function() {
    mails.push($(this).val());
});
console.log(mails); 

关于javascript - foreach 只产生一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35775106/

相关文章:

javascript - jQuery:事件不会在加载了 ajax 的元素上触发

jquery - 禁用或启用复选框选中事件上的提交按钮

jquery - Kendo UI(调度程序)- 为每个事件添加唯一的 css 类

javascript - 如何使用jquery通过引导按钮显示特定的div

javascript - 如何使这个 CSS 联系表单与 javascript 一起工作?

javascript - 如何在使用 Bootstrap (html、css)时将固定顶部更改为相对

html - 如何更改 html5 Canvas 的来源?

javascript - 请告诉我这个简单的代码有什么问题

javascript - 将选择框的值传输到隐藏字段

javascript - 如果在 iframe 中,则删除 html