- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想连接警报中的消息,以便它显示遇到错误的第一个问题的所有错误消息。例如,如果我有 3 个问题,并且问题 1 和问题 3 有错误,当我单击提交按钮时,验证警报应显示如下示例警报:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
只有在我解决了问题 1 中的错误后,当我再次提交页面时,它才会显示问题 3 的警报,如下所示:
You have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
我遇到的问题是它一次只显示 1 个问题编号,这很好,但它显示了问题 1 和 3 中的所有错误,因此如下所示:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
我的问题是如何让警报正常工作,使其符合我想要的工作方式?
下面是 jquery 代码:
function validation() {
var alertValidation = "";
var _qid = "";
var _msg = "";
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $(this).val();
_qid = questions;
_msg = "You have errors on Question Number: " + _qid + "\n";
if (txtinput == '') {
alertValidation += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox\n";
}
if(marks < '0') {
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
}
if(marks > '0') {
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
});
//comment
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}
在上面的代码中,它显示//comment
,如果我包含此代码:
if (alertValidation != "") {
return false; //Stop the each loop
}
然后它的作用是每个问题的每个警报仅显示一条消息,例如
除了警报:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
它只会提醒:
You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox
UPDATE:
以下是代码的更新:
函数验证() {
var alertValidation = "";
var _qid = "";
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $(this).val();
_qid = questions;
alertValidation += "\nYou have errors on Question Number: " + _qid + "\n";
if (txtinput == '') {
alertValidation += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox\n";
}
if(marks < '0')
{
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
}
if(marks > '0')
{
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
});
if (alertValidation != "") {
alert(alertValidation.substr(1));
return false;
}
return true;
}
但问题是它发出的警报如下:
• You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox You have errors on Question Number: 1
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
• You Have 5 Marks RemainingYou have errors on Question Number: 2
• You have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox Your Total Marks Remaining does not equal 0
• You Have 5 Marks RemainingYou have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox You have errors on Question Number: 3
• You have not entered in a value in all the Indivdiaul Marks textbox
最佳答案
您将在每次 .each()
迭代中重置 _msg
。如果您只是逐个问题地将消息的 _msg
部分添加到 alertValidation
中,应该没问题。
不确定此代码是否有效,因为我没有测试页。
function validation() {
// only keeping track of the final message
var alertValidation = "",
// toggle for showing only one error
showOnlyOneError = true;
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + (i+1) + "_ans_text]").text(), 10);
var txtinput = $(this).val();
// the message for this question
var msg = '';
if (txtinput == '') {
msg += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox\n";
}
if (marks < 0) {
msg += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
} else if (marks > 0) {
msg += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
// if there is an error for the question, add it to the main message
if (msg.length) {
alertValidation += alertValidation.length ? '\n\n' : '';
alertValidation += "You have errors on Question Number: " + questions + "\n";
alertValidation += msg;
// stop if we only care about the first error
return !showOnlyOneError;
}
});
// show the error messages
if (alertValidation != "") {
alert(alertValidation);
return false;
}
return true;
}
编辑:修复了 parseInt(...)
语句的错误。
已更新jsFiddle
关于jquery - 如何在警报中正确串联消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928034/
我正在尝试编写一行简单的代码,用于从各种服务器的 C 盘中删除各种用户文件。如何与 PowerShell 连接以获取到服务器的路径? 例如,这就是我想要做的,但 PowerShell 没有将 + 符号
我一直在尝试合并(连接)两个具有相同高度和宽度的 mp4 视频,但出现了一些错误。 代码是: FFmpeg ffmpeg = FFmpeg.getInstance(EditVideoActivity.
我正在尝试在 PostgreSQL 中进行简单的串联,但它不断抛出一条错误消息。我不明白我在这里做错了什么。 select concat('abcde', 'fgh'); No function ma
我不知道如何从一个函数中获取两个变量,这是我的菜鸟尝试,这可能吗?如果可以,它是如何完成的? #include float seriesParallel (float r1, float r2);/
我有以下代码: char stringHour[50], stringMinute[50], stringSecond[50]; // lots of code... itoa(hour, strin
我是套接字编程的新手,我正在尝试使用 TCP 实现客户端服务器。客户端在 Windows 中用 Java 编写,服务器在 Tandem/Hp-NonStop 中用 C 编写。我能够连接并向服务器发送请
到目前为止,我有以下内容。 DECLARE @Table Table (ID int, Value1 varchar(50), Value2 varchar(50), Value3 varchar(5
我是 Elasticsearch 的新手,想解决以下问题的用例 我有一些格式如下的文件 { "member_id":"number" "e_id":"number", "c_id":"numbe
我有这个功能来懒惰地观看日志文件... follow :: Handle -> IO [String] follow h = unsafeInterleaveIO $ do catch (do l
这个问题已经有答案了: List of lists changes reflected across sublists unexpectedly (17 个回答) 已关闭 3 年前。 我想创建一个没有
我们有一个 XML 文件,我们正试图找出一种动态使用的方法。 基本知识是这样的: 100.00 110.00 120.00 130.00 对于我们拥有的每个零件,我们希望在其上使用 xslt 以
我有一个很奇怪的问题。 下面的所有代码都工作正常: payload.modules.forEach(function(mod) { var modules_path = '..
如何使用 C 预处理自动生成以下代码模式(访问器)? // Immutable accessor. const auto& member1 () const { return _member1;
我有时间值来自数据库:00:30:00 这是我的代码: var gdtesttime = ; // 00:30:00 console.log(gdtesttime); var a = gdt
数据库:MySQL 我有两张表,一张用于用户分配的角色,一张包含角色信息。我的问题是分配的角色存储在单个字段中,用逗号分隔。我需要构建一个按名称而非 ID 列出角色的报告,但仍位于由列分隔的单个字段中
我在 ffmpeg 中使用以下命令,它会在视频开头添加 1 秒的黑帧。但是,我在输出视频中丢失了原始视频中的音频。我如何调整命令以确保原始音频与最终输出保持一致,或者更好的是,开头有 1 秒的“空白”
表 A 有 X 列,它是由表 B 中的 Y 列和 Z 列(均为 float )串联而成的 int。我想以类似于此的方式连接表 A 和 B: select * from tableA a inner j
pd.Series([1,1,1,1, "something", 1]).astype(float).cumsum().fillna(None) 我想在上面的系列中用 None 填充 NaN,但它告诉
在创建显示 OpenFileDialog/SaveFileDialog 的服务时,我正在考虑创建 LINQ 查询/清除 C# 代码到 Concatinate()/Join() 过滤表达式。 根据这个调
我正在尝试从数组中提取值并将所有值连接到一个字符串中,如下所示 var ingredient: String for tag in tags { if let text = tag.titleL
我是一名优秀的程序员,十分优秀!