如何在没有服务器的本地驱动器上使用 JavaScript/AJAX 验证输入值
how to validate input values using javascript/ajax on local drive without server
我将为将在本地计算机上运行HTML格式的学生分享一堂关于HTML格式的课程。我希望学习者能够检查答案,他们将这些答案写入输入中。一旦他们在本地运行它,答案表的地址栏如下所示:
file:///D:/XAMPP/htdocs/ielts/modules/reading/answers_sheet.html
输入区域如下所示:
<td class="reading">
<input type="text" name="01" class="txt " maxlength="50" value="">
</td>
<td class="reading">
<input type="text" name="02" class="txt " maxlength="50" value="">
</td>
...
........
<input type="button" class="button" name="check" id="check" value="Check your answers" onclick="submit();">
在他们输入他们的回答并单击"检查您的答案"按钮后,它应该看起来像这样:
<td class="reading">
<img src="/images/correct.gif" style="float:right;">
<input type="text" name="01" class="txt " maxlength="50" value="premises">
</td>
<td class="reading">
<img src="/images/wrong.gif" style="float:right;">
<span class="correct_anwser">(premises)</span>
</td>
你能帮我创建一个脚本来检查答案并显示它们是否正确吗?我将不胜感激有关此主题的任何反馈。
您可以将以下内容添加到 HTML 文档的底部:
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
// List your answers to match the "name" attribute of the input
// with the key ("01", "02", etc) of this object
var correctAnswers = {
"01": "alpha",
"02": "beta",
"03": "delta"
}
// Click on the check button
$("#check").on("click", function() {
// Clear results of previous submission
$(".img-box").empty();
// Loop through each element that has the "reading" class
$(".reading").each(function(index) {
// Find the input contained in each reading element
var input = $(this).find('input').first();
// Get the value for the "name" attribute
var reading = $(input).attr("name");
// Get the input value
var value = $(input).val();
// Check if the value matches the value of the element in the
// correctAnswers object
if (value == correctAnswers[reading]) {
// Insert the "correct" image if it does
$(this).prepend('<div class="img-box"><img src="/images/correct.gif" style="float:right;"></div>');
} else {
// Insert the "incorrect" image if it does not
$(this).prepend('<div class="img-box"><img src="/images/wrong.gif" style="float:right;"></div>');
}
});
});
</script>
你应该能够看到它在Plunker上工作
相关文章:
- JavaScript- AJAX
- 带有对象解析的响应javascript ajax失败
- Javascript ajax request
- 使用javascript ajax post方法的未定义偏移PHP错误
- javascript, ajax, table, colour
- Javascript/Ajax:通过点击按钮检查有效的电子邮件和电话号码
- javascript Ajax方法在不同的浏览器中有不同的结果
- 使用javascript ajax发送数组
- Javascript ajax代码在ibm worklight中不起作用
- 使用javascript(ajax,jquery?)从表中删除对象(用户等)
- 远程javascript ajax php攻击
- 由于adblock,Javascript Ajax请求在chrome中失败,我有什么选择
- 手图转换 C# Javascript/AJAX - 条形图
- Javascript/ajax 不向 php 发送空字段
- Javascript/AJAX 自动在文本文件中显示文本
- 有人可以向我解释一下代码的JavaScript / Ajax部分是做什么的
- Javascript AJAX call to Jquery Call
- 使用 php 和 javascript Ajax 发送复选框值时遇到问题
- Javascript-Ajax 无法发送任何数据
- 为什么可以't I存储在全局变量中的Javascript ajax响应