我正在尝试创建一个基于Web的测验应用程序,其中测验通过表单输入,然后输入到文本文件中。然后将分别从文本文件中将其读取给用户。我的提交按钮不起作用,但我说错了吗?我们的想法是逐行读取文本文件,调用这些行定义的值,并在单独的网页上以无线电格式显示选项。另外,我想添加一个名为“添加问题”的新按钮,它将问题和3个答案输入文本添加到表单的底部,使其响应用户。我是否必须将整个表单作为一个函数并调用它?
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile(“using theseee / this.txt ", True);
var year = document.getElementById(‘year’);
var class = document.getElementById(‘class’);
var topic = document.getElementById(‘topic’);
var title = document.getElementById(‘title’);
var question = document.getElementById(‘question’);
var option1 = document.getElementById(‘answer1’);
var option2 = document.getElementById(‘answer2’);
var option3 = document.getElementById(‘answer3’);
s.writeline(“Title: " + title);
s.writeline(“Question: " + question);
s.writeline(“Option1: " + answer1);
s.writeline(“Option2: " + answer2);
s.writeline(“Option3: " + answer3);
s.writeline("-----------------------------"); s.Close();
}
<html>
<head>
<title>Quiz Form</title>
<link rel=“stylesheet” href=“TRYstyle.css”>
</head>
<body>
<h3>Quiz form</h3>
<table>
<form onSubmit=“WriteToFile(this.txt)”>
<tr>
<td>Title</td>
<td><input type=“text” placeholder=“Title” name=“title” id=“title” maxlength=“200”/></td>
</tr>
<tr>
<td>Question</td>
<td><input type=“text” placeholder=“Question” name=“question” id=“question” maxlength=“200”/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type=“text” placeholder=“Answer” name=“answer1” id=“answer1” maxlength=“200”/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type=“text” placeholder=“Answer” name=“answer2” id=“answer2” maxlength=“200”/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type=“text” placeholder=“Answer” name=“answer3” id=“answer3” maxlength=“200”/></td>
</tr>
<tr>
<input type=“submit” value=“Submit”>
</tr>
</form>
</table>
</body>
</html>
您的HTML无效。
表单不能是表元素的子表单,输入不能是表行元素的子表单。
只有表格单元格可以是表格行的子代。
此外,您不能使用“
(LEFT DOUBLE QUOTATION MARK)来分隔属性值。只有"
(QUOTATION MARK)或'
(APOSTROPHE)。
使用a validator。
旁白:一旦你解决了这个问题,你会发现Scripting.FileSystemObject
不适用于网络浏览器。你不能用浏览器端的JS写任意文件。
你的双引号是错误的。
这是这样的:
<input type=“submit” value=“Submit”>
这是它需要的方式:
<input type="submit" value="Submit">
由于双引号,无法识别类型,默认情况下输入类型为文本。这就是你遇到这个问题的原因。
你有很多问题。
class
作为变量名称是不允许的。function WriteToFile(passForm) {
//No Idea what this is. Wont work in snippet
//set fso = CreateObject("Scripting.FileSystemObject");
//set s = fso.CreateTextFile(“using theseee / this.txt ", True);
//use the correct quotes
var year = document.getElementById('year');
//it's not allowed to use class as a variable
var classEl = document.getElementById('class');
var topic = document.getElementById('topic');
var title = document.getElementById('title');
var question = document.getElementById('question');
var option1 = document.getElementById('answer1');
var option2 = document.getElementById('answer2');
var option3 = document.getElementById('answer3');
//use the correct quotes
console.log("Title: " + title);
console.log("Question: " + question);
console.log("Option1: " + answer1);
console.log("Option2: " + answer2);
console.log("Option3: " + answer3);
console.log("-----------------------------"); s.Close();
}
<html>
<head>
<title>Quiz Form</title>
<link rel=“stylesheet” href=“TRYstyle.css”>
</head>
<body>
<h3>Quiz form</h3>
<table>
<form onSubmit="WriteToFile(this.txt)">
<tr>
<td>Title</td>
<td><input type="text" placeholder="Title" name="title" id="title" maxlength="200"/></td>
</tr>
<tr>
<td>Question</td>
<td><input type="text" placeholder="Question" name="question" id="question" maxlength="200"/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type="text" placeholder="Answer" name="answer1" id="answer1" maxlength="200"/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type="text" placeholder="Answer" name="answer2" id="answer2" maxlength="200"/></td>
</tr>
<tr>
<td>Answer</td>
<td><input type="text" placeholder="Answer" name="answer3" id="answer3" maxlength="200"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit">
</td>
</tr>
</form>
</table>
</body>
</html>