Twitter Bootstrap 2 模式表单对话框
我有以下对话框形式:
<div class='modal' id='myModal'>
<div class='modal-header'>
<a class='close' data-dismiss='modal'>×</a>
<h3>Add Tags</h3>
</div>
<div class='modal-body'>
<form accept-charset="UTF-8" action="/tagging" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="mCNvbvoPFWhD7SoJm9FPDh+BcRvCG3d16P+oOFACPuc=" /></div>
<input id="tags_string" name="tags_string" type="text" value="luca" />
<input id="id" name="id" type="hidden" value="4f1c95fd1d41c80ff200067f" />
</form>
</div>
<div class='modal-footer'>
<div class='btn btn-primary'><input name="commit" type="submit" value="Add tag" /></div>
</div>
</div>
和他的JS:
<script>
//<![CDATA[
$(function() {
// wire up the buttons to dismiss the modal when shown
$("#myModal").bind("show", function() {
$("#myModal a.btn").click(function(e) {
// do something based on which button was clicked
// we just log the contents of the link element for demo purposes
console.log("button pressed: "+$(this).html());
// hide the dialog box
$("#myModal").modal('hide');
});
});
// remove the event listeners when the dialog is hidden
$("#myModal").bind("hide", function() {
// remove event listeners on the buttons
$("#myModal a.btn").unbind();
});
// finally, wire up the actual modal functionality and show the dialog
$("#myModal").modal({
"backdrop" : "static",
"keyboard" : true,
"show" : true // this parameter ensures the modal is shown immediately
});
});
//]]>
</script>
当我单击x,即 ×
时,表单关闭让我留在当前页面,而我想进入主页。
还有“添加标签”按钮,即
不执行任何操作,同时单击键盘上的 jaust ENTER 执行此操作,我想单击“添加标签”执行相同的操作。我对 JS 和前端编程不太熟练,所以欢迎任何帮助。
I have the following dialog form :
<div class='modal' id='myModal'>
<div class='modal-header'>
<a class='close' data-dismiss='modal'>×</a>
<h3>Add Tags</h3>
</div>
<div class='modal-body'>
<form accept-charset="UTF-8" action="/tagging" data-remote="true" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="mCNvbvoPFWhD7SoJm9FPDh+BcRvCG3d16P+oOFACPuc=" /></div>
<input id="tags_string" name="tags_string" type="text" value="luca" />
<input id="id" name="id" type="hidden" value="4f1c95fd1d41c80ff200067f" />
</form>
</div>
<div class='modal-footer'>
<div class='btn btn-primary'><input name="commit" type="submit" value="Add tag" /></div>
</div>
</div>
and his JS :
<script>
//<![CDATA[
$(function() {
// wire up the buttons to dismiss the modal when shown
$("#myModal").bind("show", function() {
$("#myModal a.btn").click(function(e) {
// do something based on which button was clicked
// we just log the contents of the link element for demo purposes
console.log("button pressed: "+$(this).html());
// hide the dialog box
$("#myModal").modal('hide');
});
});
// remove the event listeners when the dialog is hidden
$("#myModal").bind("hide", function() {
// remove event listeners on the buttons
$("#myModal a.btn").unbind();
});
// finally, wire up the actual modal functionality and show the dialog
$("#myModal").modal({
"backdrop" : "static",
"keyboard" : true,
"show" : true // this parameter ensures the modal is shown immediately
});
});
//]]>
</script>
When I click x, which is <a class='close' data-dismiss='modal'>×</a>
, the form close down leaving me on the current page, while I'd like to go on the hamepage.
Also "Add tag" botton, which is <div class='btn btn-primary'><input name="commit" type="submit" value="Add tag" /></div>
don't do nothing, while clicking jaust ENTER on the keyboard do the job and I'd like clicking "Add tag" did the same.
I'm not so skilled on JS and front-end prog, so any help is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的提交按钮位于表单标签之外。
它不知道要提交什么表格。
使用 javascript 将其连接到表单。
至于应该链接到主页的
×
,为什么不直接删除data-dismiss='modal'
并使其像使用标准href='home.html'
的普通链接一样工作。下面是一些附加代码,可以为您指明使用 AJAX 提交表单的正确方向:
Your submit button is outside of the form tags.
It won't know what form to submit.
Use javascript to connect it to the form.
As for the
<a class='close' data-dismiss='modal'>×</a>
that is supposed to link to the homepage, why not just remove thedata-dismiss='modal'
and make it act like a normal link using a standardhref='home.html'
.Here is some additional code to point you in the right direction for using AJAX to submit the form:
要使提交按钮正常工作,请将其放入表单中。
但是,这会在模式底部增加意外的边距。 Bootstrap 2.0.2 引入了
modal-form
类来修复此问题,或者您可以修复自己使用如下样式定义:对于关闭模式时链接到另一个页面,我同意 TheShellfishMeme
To get the submit button work put it inside the form.
However, this adds an unexpected margin at the bottom of the modal. Bootstrap 2.0.2 introduced the
modal-form
class to fix this or you can fix it yourself with a style definition like:For linking to another page when closing the modal I go along with TheShellfishMeme
使用 HTML5,您可以拥有如下内容:
这在 HTML5 中称为 表单关联元素 当然,如果您需要支持所有浏览器+旧浏览器,那么您需要使用 JavaScript,但您可以使用 JavaScript 作为后备:)
With HTML5 you can have something like this:
This called in HTML5 form-associated element of-course if you need to support all browsers + old ones then you need to go with JavaScript, but you can use JavaScript as a fallback :)
提交表单的问题在于 bootstrap 自己的 JS 模态库 (bootstrap-modal.js) - 基本上提交事件由于第 #204 行而被阻止:
ev.preventDefault
(为什么?)。我的解决方案是添加:
但是我不知道它会产生什么问题。
The problem for submitting form lies within bootstrap own JS modal library (bootstrap-modal.js) - basicaly submit event is being prevented due to line #204:
ev.preventDefault
(why?).My solution was to add:
however I don't know what problems it will spawn.
仅供参考,您可以执行以下操作(用玉写):
FYI You can do the following (written in jade):