当jQuery ui表单对话框中的输入区域突出显示时,如何从提交表单中停止输入键? (示例代码)

时间:2012-10-14 20:06:40

标签: javascript forms jquery-ui-dialog

请参阅下面的代码了解工作演示。将打开jQuery UI模式对话框,并要求用户输入密码。如果突出显示submit按钮,一切正常。但是,如果输入区域仍然突出显示,则页面似乎重新加载,浏览器中的URL更改为example.com?confirm_password=asdfasdf#

我怀疑这是因为我在对话框中嵌入了一个表单,但这是基于一个jQuery示例。

如何解决这个问题,以便在突出显示文本输入框时按回车键相当于点击提交?

<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>

1 个答案:

答案 0 :(得分:0)

看起来提交功能仅使用按钮键(按钮提交功能仅在按钮点击时调用)。

您应该使用

明确绑定表单
$('yourformselector').submit(function(){
          $(this).dialog('close');
});