【发布时间】:2018-02-17 23:11:24
【问题描述】:
我正在尝试使用可重复使用的 jquery ui 模态对话框,在这个论坛中搜索后我找到了这样的解决方案(感谢用户 ash)
-
创建对话框类
function OkDialog() { this.showMessage = function(message) { var $dialog = $('<div></div>') .html(message) .dialog({ modal: true, closeOnEscape: true, buttons: { Ok: function() { $(this).dialog("close"); } } }); $dialog.dialog("open"); } } -
在一个公共文件(jsp)中创建一个全局对象
OK_DIALOG = new OkDialog(); -
使用所需的消息调用此函数
OK_DIALOG.showMessage("You don't have more than a single penny.");
我试过了,效果很好。
我的代码:
html:
<label>Your Name</label> : <input type="text" id="your_name"/>
<label>Your Country</label> : <input type="text" id="your_country"/>
<button type="button" id="save_button">Save</button>
jQuery :
$(document).ready(function() {
$('#save_button').on('click', function(e) {
var your_name = $('#your_name').val();
if ($.trim(your_name).length==0) {
OK_DIALOG = new OkDialog();
OK_DIALOG.showMessage("Please fill your name!");
$('#your_name').focus();
return false;
}
}
});
当我点击 save_button 时,modal 和 focus 一起工作,当我点击 ok 按钮后,焦点消失了。
那么,我的问题是如何在关闭模式后聚焦到元素?
【问题讨论】:
标签: javascript php jquery