流星:最“流星"清除表单域的方法 [英] Meteor: Most "Meteoric" way to clear form fields
问题描述
我正在使用 Meteor.js 处理示例 CRUD 应用程序,但不确定如何最好地清空表单字段.我在两个地方需要它:单击提交按钮时和单击取消按钮时.
I'm working on an example CRUD application with Meteor.js and am not sure how best to empty out the fields of a form. I need it in two places: when the Submit button is clicked, and when the Cancel button is clicked.
我通过创建一个名为 clearFormFields() 的实用程序函数来实现它,该函数仅使用 jQuery 来清空其内容,但它并没有像它应该的那样流星";我觉得它的范围应该更好,所以它没有全球可见性.我做错了什么?
I implemented it this way by creating a utility function called clearFormFields() that just uses jQuery to empty their contents, but it doesn't feel as "Meteoric" as it should; I feel it should be scoped better so it doesn't have a global visibility. What am I doing wrong?
function clearFormFields() {
$("#description").val("");
$("#priority").val("");
}
Template.todoNew.events({
'click #cancel': function(event) {
event.preventDefault();
Session.set('editing', false);
clearFormFields();
},
'submit form': function(event) {
event.preventDefault();
var theDocument = {
description: event.target.description.value,
priority: event.target.priority.value
};
if (Session.get("editing")) {
Meteor.call("updateTodo", theDocument, Session.get('theDocumentId'))
}
else {
Meteor.call("insertTodo", theDocument);
}
Session.set('editing', false);
clearFormFields();
/* Could do this twice but hate the code duplication.
description: event.target.description.value = "";
priority: event.target.priority.value = "";
*/
}
});
推荐答案
可以使用DOM表单节点原生的reset方法吗?
You could use the native reset method of the DOM form node ?
"submit form":function(event,template){
event.preventDefault();
// ...
template.find("form").reset();
}
http://www.w3schools.com/jsref/met_form_reset.asp
这篇关于流星:最“流星"清除表单域的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!