检查重复值

Check for duplicate value

本文关键字:检查      更新时间:2023-09-26

我有一个带文本字段的accordian。因此,您可以添加人员。每个人都有一个textfield,您可以在其中填写电子邮件地址。但不允许为不同的人提供重复的电子邮件地址。

例如:

个人A有电子邮件:hallo@gmail.com

个人B有电子邮件:hallo@gmail.com不允许!!

我有这个javascript:

 $('#accordion .user-row').each(function (uindex, uvalue) {
                            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>';
                        });

这是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" id="contactpersonen-email" name="contactpersonen-email"></input>
          </div>
        </div>

这是为了生成html字段:

$('#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;
                                });

我现在有这样的:

$('#add-contact p a').click(function()
                                {
                                     html = $('#new-contact').children().clone();                   
                                    // Make a copy of the first input fields
                                    var cnt = 0;
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-email-'+cnt);
         cnt++;

});

所以我得到:

联系人电子邮件contactpersonen-email-2所以这很好:)

但如果我创建了第三个文本框电子邮件。它是:

contactpersonen-email-2所以不是唯一的。

你有:

voornaam联系人联系人电子邮件。。等

因此,每个新人都必须:联系人-vornaam1、联系人-vornanam2..等

和:

联系人-email1、联系人-email2等

所以我试着这样做:

                            // Add extra contact clicked?
                                $('#add-contact p a').click(function()
                                {
var cnt = 0;
var cnt2 = 0;
var cnt3 = 0;
var cnt4 = 0;
var cnt5 = 0;
var cnt6 = 0;
          html = $('#new-contact').children().clone();                  
                                    // Make a copy of the first input fields
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-email-'+cnt);
         cnt++;

});
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-voornaam-'+cnt2);
         cnt2++;

});
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-achternaam-'+cnt3);
         cnt3++;

});
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-telefoon-'+cnt4);
         cnt4++;

});
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-functie-'+cnt5);
         cnt5++;

});
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-monteur-'+cnt6);
         cnt6++;

});



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

OK,我改成了这个:

html.find('input[type=text]').each(function () {
         me = $('#contactpersonen-email');
         me.attr('id', 'contactpersonen-email-'+cnt);
         cnt++;    
});

但现在的问题是,通过添加第三个人,它仍然给出:联系人-电子邮件-1

但现在的问题是,通过添加第三个人,它仍然给出:contactpersonen-email-2-etc

您可以这样做:

$(function(){
$('input[name^="text"]').change(function() {
    var $current = $(this);
    $('input[name^="text"]').each(function() {
        if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id'))
        {
            alert('duplicate email address. please enter another email address.');
            $current.val("");
        }
    });
  });
});

// for generating unique id example
var cnt = 0;
html.find('input[type=text]').each(function () {
         me = $(this);
         me.attr('id', 'contactpersonen-email-'+cnt);
         cnt++;
});