jQuery Ui对话框:如何防止打开多个对话框并避免只限制一个 [英] jQuery Ui Dialog: how to prevent multiple dialog openings and avoid to limit at just one
本文介绍了jQuery Ui对话框:如何防止打开多个对话框并避免只限制一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用jquery ui对话框才能打开一个对话框,并允许用户随时打开它. 我使用了以下代码,但要点是对话框只能打开一次.我不能第二次打开它.代码有什么问题?
I need to use jquery ui dialog in order to open a dialog and let users open it anytime they want. I used the following code but the point is that the dialog can open just once. I cannot open it a second time. What's wrong with the code?
$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;
}
});
});
推荐答案
方法1:
$(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>
方法2:
$(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>
看看这是否对您有帮助.
See if this helps you.
这篇关于jQuery Ui对话框:如何防止打开多个对话框并避免只限制一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文