jquery - jQuery Ui Dialog:如何防止打开多个对话框并避免仅限于一个
问题描述
我需要使用 jquery ui 对话框来打开一个对话框并让用户随时打开它。我使用了以下代码,但关键是对话框只能打开一次。我无法再次打开它。代码有什么问题?
$j(document).on("click", "p.span", function () {
$j('<div></div>').dialog({
modal: true,
closeText: 'Close',
title: "Title",
open: function () {
var markup = '<p>Text block</p>';
$j(this).html(markup);
$j(document).unbind('click');
return false;
}
});
});
解决方案
方法一:
$(document).on("click", "p span", function () { // changed p.span to p span(if you're targeting element with class span, you don't need to change this)
$('<div></div>').dialog({
modal: true,
closeText: 'Close',
title: "Title",
open: function () {
var markup = '<p>Text block</p>';
$(this).html(markup);
// remove this line if you don't want to limit it only once
$(document).off('click', 'p span'); // unbind is deprecated, use off instead
}
});
});
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p><span>Hola Amigo!</span></p>
方法二:
$(document).on("click", "p span", function () { // changed p.span to p span(if you're targeting element with class span, you don't need to change this)
// Check the default value
if($(this).attr('data-open') == 0){
$(this).attr('data-open', 1); // Change the default value
$('<div></div>').dialog({
modal: true,
closeText: 'Close',
title: "Title",
open: function () {
var markup = '<p>Text block</p>';
$(this).html(markup);
}
});
}
});
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!-- Add a custom attribute with default value 0 -->
<p><span data-open='0'>Hola Amigo!</span></p>
看看这是否对您有帮助。
推荐阅读
- javascript - 为什么我的 React 应用程序中没有呈现 Bootstrap Native 组件?
- ruby-on-rails - 为什么设计重定向我的登录请求?
- javascript - 谷歌工作箱库中的自定义获取事件?
- excel - 在执行宏时生成数字并将其传输到另一个表中,其中另一个表中没有现有数据
- javascript - auth.onAuthStateChanged 在 Firebase 中的 auth.currentUser.updateProfile 之后未触发
- python - 熊猫解析excel文件都在A列
- python - 提高图像分类模型的准确性
- android - 如何使用 iframe 在 Android 应用上播放 vemio 视频?
- jquery - 在选项卡之间自动切换 - 但在单击重新启动时
- python - 这种按位操作如何更改特定索引的单个位?