简体   繁体   English

在函数调用而不是.click()中打开模式

[英]Opening a modal in a function call instead of .click()

I have this jquery function that opens up a modal anytime a button is pressed of type 'btnClr' 我有这个jquery函数,可在每次按下“ btnClr”类型的按钮时打开模式

$(document).ready(function () {
                $('#[id*=btnClr]').onload(function () {
                    $dialog.dialog('open');
                });

                var $dialog = $("#trUI").dialog({
                    autoOpen: false,
                    title: "Edit Configurations",
                    width: "auto",
                    buttons: {
                        "Submit": function (e) {
                            AddKeyValue();
                            $(this).dialog('close');
                        },
                        "Cancel": function (e) {
                            ClearKeyVal();
                            $(this).dialog('close');
                        }
                    }
                });
            });

I'm trying to move it into a function so that it's opened anytime the function is called instead of any time the button is clicked. 我试图将其移动到函数中,以便在调用函数时(而不是在单击按钮时)打开它。

I was able to achieve this with this code 我能够用这段代码实现

function modalOpen() {

                        var $dialog = $("#trUI")
                            .dialog({
                                title: "Edit Configurations",
                                width: "auto",
                                buttons: {
                                    "Submit": function(e) {
                                        AddKeyValue();
                                        $(this).dialog('close');
                                    },
                                    "Cancel": function(e) {
                                        ClearKeyVal();
                                        $(this).dialog('close');
                                    }
                                }
                            });
            }

However, any one of the buttons will only work once. 但是,任何一个按钮只能使用一次。

Does anybody have any recommendations for how to do this? 有人对此有任何建议吗?

I believe your issue is that you also are not "opening" the dialog. 我相信您的问题是您也没有“打开”对话框。 Move $dialog.dialog('open'); 移动$ dialog.dialog('open'); into the new function you wrote, so whenever it is called it will open it. 到您编写的新函数中,因此无论何时调用它都会将其打开。

I was able to get it working. 我能够使它工作。 It was a TINY change in code. 这是代码的微小变化。 All I had to change was the call to dialog("open") 我只需要更改对dialog(“ open”)的调用

 // some function{
$("#trUI").dialog('open'); //instead of $dialog.dialog("open")
}

$(document).ready(function () {
                $("#trUI").dialog({
                    autoOpen: false,
                    title: "Edit Configurations",
                    width: "auto",
                    buttons: {
                        "Submit": function (e) {
                            AddKeyValue();
                            $(this).dialog('close');
                        },
                        "Cancel": function (e) {
                            ClearKeyVal();
                            $(this).dialog('close');
                        }
                    }
                });
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM