javascript 如何从共享点中的按钮单击关闭 SP.UI.ModalDialog?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19320690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:09:18  来源:igfitidea点击:

How to close SP.UI.ModalDialog from button click in sharepoint?

javascriptsharepoint-2010showmodaldialog

提问by mit

I want to show Confirmation Dialog when user saves any document from EDITForm.aspx. So I have written following JavaScript code.

当用户从 EDITForm.aspx 保存任何文档时,我想显示确认对话框。所以我编写了以下 JavaScript 代码。

function PreSaveAction() {

 var _html = document.createElement();
 _html.innerHTML = "  <input  type=\"button\"  value=\"Submit\"    onclick ='javascript:SubmitDlg();'  />  <input  type=\"button\"  value=\"Cancel\" onclick =\"javascript:CloseDlg();\"     /> </td>           </tr>   </tbody> </table>";


 var options = {
            title: "Confirm",
            width: 400,
            height: 200,
            showClose: false,
            allowMaximize: false,
            autoSize: false,
            html: _html           
        };
        SP.UI.ModalDialog.showModalDialog(options); 

}

function SubmitDlg() { 
   SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK);


}

function CloseDlg() {
     SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.Cancel);
}

Now I have following queries.

现在我有以下疑问。

  1. SubmitDlg and CloseDlg is not fired when clicking on Submit or Cancel.
  2. Is this right way to Submit form (SubmitDlg method ) and Cancel dialog (CloseDlg method) from modal dialog ?
  3. Also this modal dialog-box should be only appeared if no validation errors while saving record, means if any field-value is required and we have not put any value then it should display in-built red colored messages.
  1. 单击提交或取消时不会触发 SubmitDlg 和 CloseDlg。
  2. 这是从模态对话框提交表单(SubmitDlg 方法)和取消对话框(CloseDlg 方法)的正确方法吗?
  3. 此外,只有在保存记录时没有验证错误时才会出现此模式对话框,这意味着如果需要任何字段值并且我们没有输入任何值,则它应该显示内置的红色消息。

Thanks

谢谢

回答by Chaholl

in the options for the modal dialog you need to pass a reference to your call back function like this:

在模态对话框的选项中,您需要像这样传递对回调函数的引用:

var opt = SP.UI.$create_DialogOptions();
opt.width = 500;
opt.height = 200;
opt.url = url;
opt.dialogReturnValueCallback = MyDialogClosed;

SP.UI.ModalDialog.showModalDialog(opt);

Then in your callback function you can check the status:

然后在您的回调函数中,您可以检查状态:

function MyDialogClosed(result, value) {
if (result == SP.UI.DialogResult.Cancel) {
    //Cancel. Do whatever
}
else { //SP.UI.DialogResult.OK
    //User clicked OK. You can pickup whatever was sent back in 'value'    }

}

}

If you need to send stuff back from your dialog you can use this:

如果您需要从对话框中发回内容,您可以使用以下命令:

function okClicked()
{
    SP.UI.ModalDialog.commonModalDialogClose(1, someobject);
}

To make this work you'd need to hook-up a function to the OK button in your server side code using something like this:

要完成这项工作,您需要使用以下内容将功能连接到服务器端代码中的 OK 按钮:

protected override void OnLoad(EventArgs e)
    {
        if (Master is DialogMaster)
        {
            var dm = Master as DialogMaster;
            if(dm != null) dm.OkButton.Attributes.Add(@"onclick", @"return okClicked();");
        }

        base.OnLoad(e);
    }

回答by Vivek Dhanuka

add class "CloseSPPopUp" to the btn u want to click to close

将类“CloseSPPopUp”添加到您要单击以关闭的 btn

Add This Script to Page which has "CloseSPPopUp" btn

将此脚本添加到具有“CloseSPPopUp”btn 的页面

$('.CloseSPPopUp').click(function(){            
window.top.CloseSPUIPopoup();       
});

$('.CloseSPPopUp').click(function(){
window.top.CloseSPUIPopoup();
});

Now on Main Page where u are calling popup

现在在您正在调用弹出窗口的主页上

function CloseSPUIPopoup{
$(".ms-dlgContent").hide();
}

Reason: ms-dlgContent class is in Parent Page & CloseSPPopUp is in Iframe which is created at runtime

原因:ms-dlgContent 类在父页面中,CloseSPPopUp 在运行时创建的 Iframe 中