我当前正在尝试将任务添加到数组后清除输入字段。我知道这将非常简单,但对于我的一生,我似乎无法找出解决方案。任何帮助将不胜感激。谢谢。
HTML:
<body>
<form action="#" method="post" id="theForm">
<fieldset><legend>Enter an Item To Be Done</legend>
<div>
<label for="task">Task</label>
<input type="text" name="task" id="task" required>
</div>
<div>
<input type="submit" value="Add It!" id="submit" value="reset form">
</div>
<div id="output"></div>
</fieldset>
</form>
</body>
</html>
JavaScript:
var tasks = [];
function addTask() {
'use strict';
var task = document.getElementById('task');
var output = document.getElementById('output');
var message = '';
if (task.value) {
tasks.push(task.value);
message = '<h2>To-Do</h2>';
message += '<ol><li>';
message += tasks.join('</li><li>');
message += '</li></ol>';
output.innerHTML = message;
}
return false;
}
function init() {
'use strict';
document.getElementById('theForm').onsubmit = addTask
frm.submit();
frm.reset();
return false;
}
window.onload = init;
答案 0 :(得分:1)
只需添加task.value ='';清除任务字段的值即可
这是完整的工作代码。
HTML
<form action="#" method="post" id="theForm">
<fieldset><legend>Enter an Item To Be Done</legend>
<div>
<label for="task">Task</label>
<input type="text" name="task" id="task" required>
</div>
<div>
<input type="submit" value="Add It!" id="submit" value="reset form">
</div>
<div id="output"></div>
</fieldset>
</form>
</body>
</html>
JavaScript:
var tasks = [];
function addTask() {
'use strict';
var task = document.getElementById('task');
var output = document.getElementById('output');
var message = '';
if (task.value) {
tasks.push(task.value);
message = '<h2>To-Do</h2>';
message += '<ol><li>';
message += tasks.join('</li><li>');
message += '</li></ol>';
task.value='';
output.innerHTML = message;
}
return false;
}
function init() {
'use strict';
document.getElementById('theForm').onsubmit = addTask
frm.submit();
frm.reset();
return false;
}
window.onload = init;
答案 1 :(得分:0)
对我来说,看来您只需要添加1个字符串
output.innerHTML = message;
task.value = '';