简体   繁体   English

如何在Meteor.js中添加提交按钮?

[英]How to add a submit button in Meteor.js?

Template.feed.events = ({

// Press enter to submit the post.
'keypress, .posttext':function(evt,tmpl){
    if(evt.which == 13){
        var posttext = tmpl.find('.posttext').value;
        var options = {text:posttext,parent:null};
        Meteor.call('addPost',options);
        $('.posttext').val("").select().focus();
    }
}

I am not so good with Meteor or javascript, are there any good resources that will break down Meteor into a basic understanding? 我对Meteor或javascript不太满意,是否有任何好的资源可以将Meteor分解为基本知识? Thanks! 谢谢!

Here's the correct way of handling a submit button: 这是处理提交按钮的正确方法:

// client/yourtemplate.html
<template name="yourForm">
  <form>
    <input type="text" class="posttext">
    <input type="submit" value="Submit post">
  </form>
</template>

// client/yourfile.js
Template.feed.events = {
  // "submit form" handles all types of form submission: pressing Enter in
  // the input text field, clicking the button, or tabbing to it then
  // pressing Enter
  'submit form': function (event, template) {
    event.preventDefault();  // disable the browser's form submission
    var posttext = template.find('.posttext').value;
    ...
  }
};

Note that in the low-level keypress approach, you had an extra comma between keypress and .posttext . 请注意,在低级keypress方法中, keypress.posttext之间有一个逗号。 The even selector syntax is eventtype selector , and commas separate different selectors . 偶数选择器语法是eventtype selector逗号分隔不同的选择器

That said, welcome to Meteor! 也就是说,欢迎来到流星! A few pointers: 一些提示:

  1. Try to follow the Meteor style guide and place a space after punctuation (see more at https://github.com/meteor/meteor/wiki/Meteor-Style-Guide ) 尝试遵循Meteor样式指南并在标点符号后放置一个空格(有关更多信息, 参见https://github.com/meteor/meteor/wiki/Meteor-Style-Guide
  2. There is an excellent resource for learning the very basics of JavaScript and Meteor at the same time: David Turnbull's Complete Beginner's Guide to the Meteor JavaScript Framework . 有一个很好的资源可以同时学习JavaScript和Meteor的基本知识:David Turnbull的《 Meteor JavaScript Framework入门指南》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM