Meteor:具有两个提交按钮的表单(确定事件处理程序中单击的按钮)

Meteor: form with two submit buttons (determine button clicked in event handler)

本文关键字:按钮 表单 事件处理 单击 程序 提交 两个 Meteor      更新时间:2023-10-19

我有一个简单的表单,有两个输入:"title"和_"description",还有两个按钮:"save"(保存以备稍后使用)和"submit"。对于这两种情况,我都希望获得表单字段的值,并相应地插入/更新集合。

<template name="NewScenarioForm">
  <form id="newScenarioForm" >
    <textarea type="text" id="title" name="title" rows="1" cols="75" placeholder="Type to add a title"></textarea><br/>
    <textarea type="text" id="description" name="description" rows="4" cols="100" placeholder="Type to add a description" ></textarea><br/>
    <input type="submit" id="saveScenarioButton" name="action" title="Save Scenario" value="Save" />
    <input type="submit" id="submitScenarioButton" name="action" title="Submit for approval" value="Submit" />       
 </form>
</template>

现在我正在检测这样的事件:

"click #saveScenarioButton": function(event, template) {
    event.preventDefault(); 
    var title = template.find("#title").value;
    var description = template.find("#description").value;
    ...
   //Do stuff with this information to persist information
   Meteor.call("saveScenario", title, description);
   ....
 }

我需要为另一个按钮重复整个功能。我想检测事件并确定按下了哪个按钮。

我一直在为一个事件处理程序而挣扎,比如:

"submit #newScenarioForm": function(event) { 

但是我不知道如何确定点击的按钮,因为我无法计算出事件属性。如果我想在事件处理程序中使用表单ID而不是每个按钮的ID,有没有一种方法可以确定按钮(或者一种更聪明的方法来处理这个问题?)?

您可以使用submit类型使事件目标输入:

Template.NewScenarioForm.events({
    "click input[type=submit]": function(e) {
        if ($(e.target).prop("id") == "saveScenarioButton") {
            // Save the scenario
        } else if ($(e.target).prop("id") == "submitScenarioButton") {
            // Submit the scenario
        }
    }
});

你也可以让它检查点击按钮的值,并删除ID字段

请注意,这不会处理其他提交表单的方式,例如用户在输入字段中按Enter键。处理这一问题的方法也可以是定义几个函数:

function scrapeForm() {
    // Collects data from the form into an object
}
function saveData() {
    var formData = scrapeForm();
    // More logic to save the data
}
function submitData() {
    var formData = scrapeForm();
    // More logic to submit the data
}
Template.NewScenarioForm.events({
    "click input[type=submit]": function(e) {
        if ($(e.target).prop("id") == "saveScenarioButton") {
            saveData();
        } else if ($(e.target).prop("id") == "submitScenarioButton") {
            submitData();
        }
    },
    "submit #NewScenarioForm":
        // Default action on submit.
        // Either save the data
        saveData
        // or submit the data
        submitData
        // or nothing, requiring the user to actually click one of the buttons
        function(e) {e.preventDefault();}
});

为什么不给它们两个像submitForm 一样的类

<input class="submitForm"** type="submit" id="saveScenarioButton" name="action" title="Save Scenario" value="Save" />
<input class="submitForm" type="submit" id="submitScenarioButton" name="action" title="Submit for approval" value="Submit" />

然后点击.submitForm,如下所示:

$('.submitForm').on('click', function () {...});

在函数内部执行以下操作获取id:

var id = $(this).attr('id');

完整代码:

$('.submitForm').on('click', function () {
    var id = $(this).attr('id');
    ... the rest of your code ...
});

我这样做是为了使用classid正确识别按钮。

helloWorld.html

<head>
  <title>helloWorld</title>
</head>
<body>
  <h1>Welcome to Meteor!</h1>
  {{> hello}}
</body>
<template name="hello">
  <button class="plus5">+5</button>
  <button class="minu5">-5</button>
  <button id="plus1">+1</button>
  <button id="minu1">-1</button>
  <p>You've pressed the button {{counter}} times.</p>
</template>

helloWorld.js

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);
  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });
  Template.hello.events({
    'click button.plus5': function () { 
      Session.set('counter', Session.get('counter') + 5);
    },
    'click button.minu5': function () {
      Session.set('counter', Session.get('counter') - 5);
    },
    'click button#plus1': function () {
      Session.set('counter', Session.get('counter') + 1);
    },
    'click button#minu1': function () {
      Session.set('counter', Session.get('counter') - 1);
    }
  });
}
if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

CCD_ 5、CCD_。