作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在仅在一个警报框中构建我的消息时遇到了麻烦。我已尽力构建,但这就是我所拥有的一切。希望有人可以帮助我并解释一下你的答案,因为我对连接以及我做错了什么感到困惑。
代码:
$(function(){
var len = $('#groupContainer > div').length;
var data = [];
for(var i=0; i < len; i++){
var number = $('#name_' + [i + 1]);
var date = $('#date_' + [i + 1]);
var count = i + 1;
var message ="";
var a = number.map(function(){
return this.value;
});
var b = date.map(function(){
return this.value;
});
var newObj = {Name: a[0], Date: b[0]}
data.push(newObj);
}
var message = "";
var ErrInGroup = false;
for(var i = 0; i < data.length; i++)
{
for(var d in data[i]) {
if(data[i].hasOwnProperty(d)) {
message += 'Group: ' + [i + 1] + '\n';
if(data[i][d] == "" || data[i][d] == null){
message += d + ' is required!\n';
}
if(d == "Date") {
if(data[i][d].length != 10 && data[i][d] != "") {
message += 'Invalid Date!\n';
}
}
}
}
}
if(message){
alert(message);
}
});
预期输出:
<强> Fiddle Demo
注意:日期的长度为10,如果!=到10则无效。
最佳答案
请检查[
$(function(){
var errorObj = {
group: {},
hasError: false
};
function errorObjMange(groupNo, type, msg) {
errorObj.hasError = true;
groupNo = groupNo.toString();
if (!errorObj.group[groupNo]) {
errorObj.group[groupNo] = {};
}
errorObj.group[groupNo][type] = msg;
}
function alertError() {
var priority = {
"1": new Array(), // Invalid date
"2": new Array(), // empty name and inavlid date
"3": new Array(), // empty naem and empty date
"4": new Array(), // only empty name
"5": new Array(), // only empty DATE
}
for (var groupIndex in errorObj.group) {
var group = errorObj.group[groupIndex];
if (!group.emptyName && !group.emptyDate && group.invalidDate) {
priority["1"].push(groupIndex);
}
if (group.emptyName && !group.emptyDate && group.invalidDate) {
priority["2"].push(groupIndex);
}
if (group.emptyName && group.emptyDate && !group.invalidDate) {
priority["3"].push(groupIndex);
}
if (group.emptyName && !group.emptyDat && !group.invalidDate) {
priority["4"].push(groupIndex);
}
if (!group.emptyName && group.emptyDate && !group.invalidDate) {
priority["5"].push(groupIndex);
}
}
var groupStr = "Group: ";
var msg = '';
if (priority["3"].length) {
groupStr += priority["3"].toString();
msg = groupStr + '\n'
+ 'Name is required\n'
+ 'Date is required\n';
} else if (priority["2"].length) {
groupStr += priority["2"].toString();
msg = groupStr + '\n'
+ 'Name is required\n'
+ 'Invalid Date\n';
} else if (priority["4"].length || priority["5"].length) {
msg = '';
if (priority["4"].length) {
groupStr = 'Group :' + priority["4"].toString();
msg += groupStr + '\n'
+ 'Name is required\n';
}
if (priority["5"].length) {
groupStr = 'Group :' + priority["5"].toString();
msg += groupStr + '\n'
+ 'Date is required\n';
}
} else if (priority["1"].length) {
groupStr += priority["1"].toString();
msg = groupStr + '\n'
+ 'Invalid Date\n';
}
console.log();
alert(msg);
}
// Count number of groups we have.
var len = $('#groupContainer > div').length;
var data = [];
// For each given group
for(var i=0; i < len; i++){
// Get the values
var number = $('#name_' + [i + 1]);
var date = $('#date_' + [i + 1]);
var a = number.map(function(){
return this.value;
});
var b = date.map(function(){
return this.value;
});
var newObj = {Name: a[0], Date: b[0]};
data.push(newObj);
}
var message = "";
var ErrInGroup = false;
for(var i = 0; i < data.length; i++) {
haveErrorInGroup = false;
for(var d in data[i]) {
if(data[i].hasOwnProperty(d)) {
// Its because you are doing the
// below line.
//message += [i + 1] + '\n';
if(data[i][d] == "" || data[i][d] == null){
if (d == 'Name') {
errorObjMange((i+1), 'emptyName', (d + ' is required!\n'));
}
if (d == 'Date') {
errorObjMange((i+1), 'emptyDate', (d + ' is required!\n'));
}
}
if(d == "Date" && !(data[i][d] == "" || data[i][d] == null)) {
if(!(new Date(data[i][d]))
|| !(/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(data[i][d].trim()))) {
message += 'Invalid Date!\n';
errorObjMange((i+1), 'invalidDate', 'Invalid Date!\n');
}
}
}
}
}
if(errorObj.hasError){
alertError();
//console.log(errorObj);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="groupContainer">
<div id ="profileGroup1">
<div class="item">
Name1: <input type="text" id="name_1" value="asdf">
</div>
<div class="item">
Date1: <input type="text" id="date_1" value="01/50/48">
</div>
</div>
<div id ="profileGroup2">
<div class="item">
Number2: <input type="text" id="name_2" value="">
</div>
<div class="item">
Date2: <input type="text" id="date_2" value="">
</div>
</div>
关于javascript - 如何在js中使用条件连接消息或仅在一个警报框中构造消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072198/
我是一名优秀的程序员,十分优秀!