[英]How to stop enter key from submitting form when input area in jQuery ui form dialog is highlighted? (sample code)
请参阅下面的代码以获取有效的演示。 jQuery UI模式对话框打开,并要求用户输入密码。 如果突出显示了submit
按钮,则一切正常。 但是,如果输入区域仍突出显示,则该页面似乎正在重新加载,并且浏览器中的URL更改为example.com?confirm_password=asdfasdf#
。
我怀疑这是因为我在对话框中嵌入了一个表单,但这是基于jQuery示例的。
如何解决此问题,以便在突出显示文本输入框的情况下按Enter键等同于单击“提交”?
<html>
<head>
<title>Problem Demo</title>
<link href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#myDialog').dialog({
autoOpen: false,
modal: true,
buttons: {
"Submit": function() {
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
}
});
$('#openMyDialog').click(function() {
$('#myDialog').dialog('open');
});
});
</script>
</head>
<body>
<a id="openMyDialog" href="#">Open Dialog</a>
<div id="myDialog">
<p style="text-align: left;">Please enter your password.</p>
<form>
<fieldset>
<label for="confirm_password">Password</label>
<input type="password" name="confirm_password" id="confirm_password" value="" />
</fieldset>
</form>
</div>
</body>
</html>
看起来Submit函数仅在使用按钮键时才起作用(按钮Submit函数仅在单击按钮时被调用)。
您应该使用以下方式显式绑定表单
$('yourformselector').submit(function(){
$(this).dialog('close');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.