[英]Meteor change value on modal window when editing
I would like to add 'Edit' function using modal dialog in a meteor app. 我想在流星应用中使用模式对话框添加“编辑”功能。 In .html I have 在.html中,我有
<template name="edit_deal">
<button id="edit_btn" class="edit">Edit</button>
<div id="edit_dialog" title="Edit opportunity">
{{> Edit }}
</div>
</template>
<template name="Edit">
<form class="edit-deal">
<input type="text" name="cuname" placeholder="Customer Name">
</form>
</template>
in .js I have 在.js我有
Template.edit_deal.rendered = function() {
$( "#edit_dialog" ).dialog({
height: 240,
width: 800,
modal: true,
resizable: false,
autoOpen: false,
dialogClass: 'no-close add-dialog'
});
}
Template.edit_deal.events({
'click #edit_btn': function(event, template) {
$( "#edit_dialog" ).dialog('open');
}
});
Template.Edit.events({
"submit .edit-deal": function (event) {
event.preventDefault();
var cuname = event.target.cuname.value;
Deals.insert({
customerName: cuname
});
}
});
In the Template.edit_deal.events I can get the current name, but cannot write it back to the form. 在Template.edit_deal.events中,我可以获得当前名称,但是无法将其写回到表单中。 How can I change the form value? 如何更改表格值?
You can use jQuery's .val() method to set the value of the input field in your modal: 您可以使用jQuery的.val()方法来设置模态中输入字段的值:
Template.edit_deal.events({
'click #edit_btn': function(event, template) {
$("#edit_dialog").dialog('open');
$('input[name="cuname"]').val("foo bar");
}
});
However, I personally prefer to create a helper function which sets the HTML input
value
attribute: 但是,我个人更喜欢创建一个辅助函数来设置HTML input
value
属性:
Template.Edit.helpers({
cunameValue: function() {
return "foo bar";
}
});
<template name="Edit">
<form class="edit-deal">
<input type="text" name="cuname" placeholder="Customer Name" value="{{cunameValue}}">
</form>
</template>
Please note that Template.edit_deal.rendered = function(){};
请注意, Template.edit_deal.rendered = function(){};
is deprecated since Meteor version 1.0.4, as a consequence you should use Template.edit_deal.onRendered(function(){});
从Meteor版本1.0.4开始不推荐使用,因此您应该使用Template.edit_deal.onRendered(function(){});
instead. 代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.