我有一个带有两个输入的简单表单:“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(或者更智能的方法来完全解决这个问题?)?
答案 0 :(得分:3)
您可以使用提交类型生成事件目标输入:
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();}
});
答案 1 :(得分:1)
为什么不给他们提供像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
一样onClick:
$('.submitForm').on('click', function () {...});
并在函数内部通过执行以下操作获取id:
var id = $(this).attr('id');
完整代码:
$('.submitForm').on('click', function () {
var id = $(this).attr('id');
... the rest of your code ...
});
答案 2 :(得分:1)
我这样做是为了使用class
或id
正确识别按钮。
<强>的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
});
}
click .plus5
,click #plus1
也可以。