【问题标题】:jQuery UI for form confirmation用于表单确认的 jQuery UI
【发布时间】:2012-02-13 03:04:11
【问题描述】:

这是我在 StackOverflow 上的第一个问题,我已经访问了有关 jQuery 对话框和表单的每一个问题,但仍有一个问题。

我正在尝试在我的表单上实现一个 jQuery 确认对话框。窗口实际上没有问题出现,但是当我点击“确认”时,表单没有提交......

这是Javascript:

        // Form validation
        $(function(){
            var currentForm;
            $("#dialog-confirm").dialog({
                    autoOpen: false,
                    resizable: false,
                    modal: true,
                    buttons: {
                        'Confirm': function(e) {
                            currentForm.submit();
                            return true;
                        },
                        Cancel: function() {
                            $(this).dialog('close');
                            return false;
                        }
                    }
                });
            $("#news-listing").submit(function(e){
                currentForm=$(this);
                if($("#dialog-confirm").dialog("open"))
                {
                    return false;
                } 
            });
});

还有 HTML

<div id="dialog-confirm" title="Empty the recycle bin ?">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin: 20px 10px 20px 10px;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

<form action="news/action/delete" id="news-listing">
<table id="users-listing">
    <tr>
        <th class="id">ID</th>
        <th class="title">Title</th>
        <th class="url">URL</th>
        <th class="modify">Modify</th>
        <th class="checkcell">Delete</th>
    </tr>   
    <tr class='alt'>
        <td class='id'>".$news['news_id']."</td>";
        <td class='title'>".$news['news_title']."</td>";
        <td>".$this->News->get_url($news['news_id'])."</td>";
        <td class='modify'><a href="modify/1">Modify</a></td>;
        <td><input type="checkbox" name="delete[]" /></td>
    </tr>
</table>
<input type="submit" name="submit" />
</form>

提前感谢您的帮助!

编辑: 这是我的新代码,HTML 没有改变。现在我有了对话框,但确认按钮没有提交,取消按钮效果很好。

$(function() {   
                // Get the current form object 
                var currentForm = $("#news-listing");
                // Initialize dialog
                $("#dialog-confirm").dialog({
                    autoOpen: false,
                    resizable: false,
                    modal: true,
                    buttons: {
                        'Confirm': function() {
                            currentForm.submit();
                            return true;
                        },
                        Cancel: function() {
                            $(this).dialog('close');
                            return false;
                        }
                    }
                });

                // Validate the form
                $("#news-listing").validate({
                    rules:{
                        "delete[]":"required"
                    },
                    submitHandler: function(e){
                        $("#dialog-confirm").dialog("open");
                    }
                });
            });

【问题讨论】:

  • 您是否查看过 Firefox、Chrome 或 Safari 的错误控制台?也许你犯了一个 javascript 错误。

标签: jquery forms user-interface dialog confirmation


【解决方案1】:

问题是currentForm在执行此行时是undefined

currentForm.submit();

因为undefined没有submit方法会抛出错误!

您尝试在表单本身的submit 事件处理程序中设置currentForm 的值,因此您遇到了鸡与蛋的问题。您需要在事件处理程序之外将表单元素分配给currentForm

$(function() {    
    var currentForm = $("#news-listing");
    $("#dialog-confirm").dialog({
        //Dialog options...
        //currentForm.submit() will now work here
    });
    $("#news-listing").submit(function(e) {
        //Event handler...
        //This will be executed when currentForm.submit() is called
    });
});

【讨论】:

  • 感谢您的宝贵时间!您的解决方案删除了​​“未定义”错误,但“确认”按钮仍然没有提交表单,没有抛出任何错误。
  • submit 事件处理程序的作用是什么?目前它总是返回false。如果要检查对话框是否打开,您需要isOpen 方法,而不是open
  • 提交处理程序必须打开对话框进行确认,如果单击“确认”,则它实际上是提交表单。
  • 好的,我找到了答案:只需将“currentForm.submit()”替换为“document.getElementById().submit()”即可。我不知道为什么,但是 Dialog 方法中的 jQuery Selector 不可用...
【解决方案2】:

currentForm 变量需要用表单元素初始化。

改变

var currentForm;

var currentForm = document.getElementById("news-listing");

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 1970-01-01
    • 2011-04-22
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多