我正在关注 Meteor Tuturial ( https://www.meteor.com/tutorials/blaze/creating-an-app ),以下是 HTML 和 JavaScript 代码的部分内容:
HTML
<body>
<div class="container">`enter code here`
<header>
<h1>Todo List</h1>
<form class="new-task">
<input type="text" name="text" placeholder="Type to add new tasks" />
</form>
</header>
<ul>
{{#each tasks}}
{{> task}}
{{/each}}
</ul>
</div>
</body>
JavaScript
Template.body.events({
"submit .new-task": function (event) {
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var text = event.target.text.value;
// Insert a task into the collection
Tasks.insert({
text: text,
createdAt: new Date() // current time
});
// Clear form
event.target.text.value = "";
}
});
我想知道如何在 HTML 表单中添加另一个输入并在 Java 脚本中访问该另一个输入。
例如:
<input type="text" name="city" placeholder="Type to add city" />
最佳答案
如果将示例输入插入 HTML,新的 javascript 应该是:
Template.body.events({
"submit .new-task": function (event) {
// Prevent default browser form submit
event.preventDefault();
// Get value from form element
var text = event.target.text.value;
var city = event.target.city.value;
// Insert a task into the collection
Tasks.insert({
text: text,
createdAt: new Date() // current time
});
Tasks.insert({
text: city,
createdAt: new Date() // current time
});
// Clear form
event.target.text.value = "";
event.target.city.value="";
}
关于javascript - 如何从 HTML 输入中获取多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35515627/