流星:大多数"流星"的方式来清除表格领域

tom*_*cam 3 meteor

我正在使用Meteor.js编写示例CRUD应用程序,并且不确定如何最好地清空表单的字段.我在两个地方需要它:单击"提交"按钮时,以及单击"取消"按钮时.

我通过创建一个名为clearFormFields()的实用程序函数来实现它,它只使用jQuery来清空它们的内容,但它并不像它应该感觉到的那样是"流星"; 我认为它应该更好地限制,因此它没有全局可见性.我究竟做错了什么?

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 = "";
        */
    }
});
Run Code Online (Sandbox Code Playgroud)

sai*_*unt 15

您可以使用DOM表单节点的本机重置方法吗?

"submit form":function(event,template){
  event.preventDefault();
  // ...
  template.find("form").reset();
}
Run Code Online (Sandbox Code Playgroud)

http://www.w3schools.com/jsref/met_form_reset.asp

  • 引用http://docs.meteor.com/#/full/eventmaps"处理程序函数接收两个参数:event,一个包含事件信息的对象,以及模板,定义处理程序的模板的模板实例." (3认同)