foreach 只产生一个结果
foreach produces just one result
我有三个电子邮件文本框,里面有三个不同的值。但它会产生相同的结果。我有这个:
$('.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"
});
});
]]>
所以谢谢你。带有验证的 Als。和手风琴。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);
你不需要外部each
。map()
已经遍历所有元素,因此您实际上正在多次创建相同的数组
只需使用:
var mails = $('.contactpersonen-email').map(function(){
return $(this).val();
}).get();
console.log(mails);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="input-text span2 contactpersonen-email" value="email 1">
<input type="text" class="input-text span2 contactpersonen-email" value="email 2">
<input type="text" class="input-text span2 contactpersonen-email" value="email 3">
相关文章:
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 使用NextToken使用javascript aws-sdk解析下一个结果
- Angular.forEach 只返回一个结果
- 谷歌地图商店定位器只返回一个结果
- .each() 函数只输出数组中的最后一个结果
- 如何通过PHP或JavaScript将整个DIV数据发送到下一个结果页面
- 将一个结果集除以另一个结果集,然后显示答案
- foreach 只产生一个结果
- 查找主干集合上的位置仅返回一个结果
- 插入数据库时如何从数据库中检索最后一个结果
- 在 JavaScript 中添加表值会在 NaN 的表单元格中给我一个结果
- 如何将一个结果列表合并到一个包含汇总的压缩对象中
- java脚本regex.match只查找一个结果
- 输出一个结果数组,其中包含给定数组的唯一元素的分组计数
- 从XML文件中隔离一个结果
- 骨干收集发现我只想要一个结果
- 我需要用AJAX Post声明一个结果类型吗
- 在另一个结果中的单选按钮
- 我试图从一个函数内的函数返回一个结果
- 在Javascript页面中搜索-一个结果错误