我在下面列出了Marionette应用程序,然后是HTML。我想这样做,以便当按下“处理”按钮时表单将从页面中消失。在hideForm方法中,我尝试了一些隐藏click事件表单的东西。在这些尝试中,只有这个命令“$('。form')。hide()”才有效。问题是它只是部分起作用,一旦单击按钮,表单消失但随后立即重新出现。最后我想知道我做错了什么,但如果有人能告诉我为什么我在hideform方法中的其他方法什么都不做我会喜欢解释。
MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
formBox : '#formBox',
listBox : '#listBox'
});
Entry = Backbone.Model.extend({
defaults: {
entry : 'Blank'
},
});
EntryList = Backbone.Collection.extend({
model: Entry
});
FormView = Backbone.Marionette.ItemView.extend({
tagName: 'form',
template: '#form-template',
className: 'form',
events:{
'click #processInput' : 'hideForm'
},
hideForm : function(){
//$('.form').css('display','none')
//document.getElementById("form").style.display="none";
$('.form').hide();
}
});
EntryView = Backbone.Marionette.ItemView.extend({
tagName: 'tr',
template: '#entry-template',
className: 'entry',
events: {
'click .delete' : 'destroy'
},
destroy : function()
{
this.model.destroy();
}
});
EntriesView = Backbone.Marionette.CompositeView.extend({
tagName: 'table',
template: '#entries-template',
itemView: EntryView,
appendHtml: function(collectionView, itemView){
collectionView.$('tbody').append(itemView.el);
}
});
MyApp.addInitializer(function(test){
var entriesView = new EntriesView({
collection: test.entry
});
var formView = new FormView();
MyApp.formBox.show(formView);
MyApp.listBox.show(entriesView);
});
$(document).ready(function(){
var ents = new EntryList([
new Entry({ entry: 'test a' }),
new Entry({ entry: 'test b' }),
new Entry({ entry: 'test c' })
]);
MyApp.start({entry: ents});
});
HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="assets/screen.css">
<title>Simple Demo</title>
</head>
<body>
<div id = "formBox">
</div>
<div id = "listBox">
</div>
<script type="text/template" id="form-template">
<input id = "a" placeholder = "a" autofocus>
<br />
<input id = "b" placeholder = "b">
<br />
<textarea id = "c" placeholder = "c"></textarea>
<br />
<button id = "processInput" >process</button>
</script>
<script type="text/template" id="entries-template">
<thead>
<tr class='header'>
<th>Entry</th>
</tr>
</thead>
<tbody>
</tbody>
</script>
<script type="text/template" id="entry-template">
<td><%- entry %></td>
<td><button class="delete">Delete</button></td>
</script>
<script src="js/lib/jquery.js"></script>
<script src="js/lib/underscore.js"></script>
<script src="js/lib/backbone.js"></script>
<script src="js/lib/backbone.marionette.js"></script>
<script src="js/demo.js"></script>
</body>
答案 0 :(得分:0)
正如您在此处所见:http://jsfiddle.net/9Vp3A/
按钮似乎是隐式“提交”类型。返回false会解决问题。
hideForm : function(){
//$('.form').css('display','none')
//document.getElementById("form").style.display="none";
//$('.form').hide();
this.$el.hide(); // This is cached on the object. Best to use it.
return false;
}
答案 1 :(得分:0)
尝试:
$('.form').css({"display":"none"});
这会隐藏表单,就像代码中不存在一样。
或(@ Makis192解决方案)
$('.form').style.visibility = "hidden";
这将隐藏来自但仍显示空白