簡體   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();
    }
}

我對Meteor或javascript不太滿意,是否有任何好的資源可以將Meteor分解為基本知識? 謝謝!

這是處理提交按鈕的正確方法:

// 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;
    ...
  }
};

請注意,在低級keypress方法中, keypress.posttext之間有一個逗號。 偶數選擇器語法是eventtype selector逗號分隔不同的選擇器

也就是說,歡迎來到流星! 一些提示:

  1. 嘗試遵循Meteor樣式指南並在標點符號后放置一個空格(有關更多信息, 參見https://github.com/meteor/meteor/wiki/Meteor-Style-Guide
  2. 有一個很好的資源可以同時學習JavaScript和Meteor的基本知識:David Turnbull的《 Meteor JavaScript Framework入門指南》

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM