检查重复值
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++;
});
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 如何检查管道中未定义的项目
- 主干-不管怎样,检查事件以前是否绑定过
- 使用javascript检查多个输入值,并在1次检查中标记多个输入框
- 递归使用 eval() 是检查程序执行的好方法吗?
- 检查onsubmit doenst work jquery contactform上的函数
- JavaScript 检查隐藏字段中的值并将其设置为隐藏字段值
- API密钥使用和检查示例
- 如何让程序检查所选单词中是否有按键
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 用于检查数组中是否存在元素的javascript自定义方法
- 如何检查字符串的一部分与数组匹配
- Jquery如何检查今天的时间大于使用给定时间
- 使用javascript反复检查用户在facebook上的登录状态
- 使用 jQuery 检查所有值是否为空或已填充
- 使用Javascript使用数组检查文本框中的值
- JavaScript代码未正确检查ajax请求
- 如何检查用户在html5视频播放器中观看了完整的视频
- 从应用程序脚本检查谷歌网站访问权限