Meteor:具有两个提交按钮的表单(确定事件处理程序中单击的按钮)
Meteor: form with two submit buttons (determine button clicked in event handler)
我有一个简单的表单,有两个输入:"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 ...
});
我这样做是为了使用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
});
}
CCD_ 5、CCD_。
相关文章:
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 点击相同的按钮打开模型,然后提交表单
- 为什么我的按钮组件提交它所在的表单
- 在表单完成并确认密码之前,请禁用提交按钮
- 输入文本框为空时的阻止按钮asp.网络表单
- 使用按钮和单选按钮提交表单,但始终使用相同的值
- HTML4表单使用mattw@scriptarchive.comcgi脚本我可以嵌入贝宝按钮
- 单击表单中的按钮会导致页面刷新
- 从输入时提交到jQuery,无需提交按钮,无需表单操作
- 单选按钮表单代码和下拉菜单代码
- 在单选按钮表单选项上附加链接
- 如果电子邮件输入处于焦点上,则阻止输入按钮表单提交 (jquery)
- jquery动态选择单选按钮(表单重置问题)
- Jquery History/Hashchange Event-返回按钮-表单提交时
- 创建第二个addtocart按钮/表单
- HTML/Javascript单选按钮表单,重定向到不同的链接
- 停止不必要的按钮表单提交html js
- JSF提交按钮-表单在一秒钟内被提交X次(直到重新创建视图)-如何防止它
- next和prev按钮表单流的HTML表单验证
- 如何在提交无按钮表单时激发函数