我正在尝试创建一个基于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无效。
表单不能是表元素的孩子,并且输入不能是表行元素的孩子。
只有表单元才能是表行的孩子。
此外,您不能使用 “
(左双引号)来划界属性值。仅"
(引号)或'
(postrophe)。
使用验证器。
旁边:解决该问题后,您会发现Scripting.FileSystemObject
无法用于Web浏览器。您不能用浏览器侧JS写入任意文件。
您的双重引用是错误的。
这就是:
<input type=“submit” value=“Submit”>
这就是需要的方式:
<input type="submit" value="Submit">
由于双重引用,因此未识别类型,默认情况下输入类型为文本。那就是为什么您遇到这个问题。
您有多个问题。
- 首先使用正确的引号有助于很多问题。
- 将您的提交按钮放入TD元素中,以免将其直接放入行中。
- 您不允许使用
class
作为JavaScript中的变量名称。
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>