简体   繁体   English

jquery对话框在按钮单击时打开新实例

[英]jquery dialog open new instance on button click

I am trying to open multiple dialog windows with each button click...I can't figure this out and have researched but couldnt find any info. 我试图打开多个对话框窗口,每个按钮单击...我无法弄清楚,已经研究但无法找到任何信息。

code: 码:

$('.dvclick').click(function(e) {

    var title = $(this).attr('id');

    $("#info_window").dialog({
        width: 200,
        title: title
    });
});

Fiddle: http://jsfiddle.net/Lj418jm5/2/ 小提琴: http//jsfiddle.net/Lj418jm5/2/

From the fiddle what i want to happen is each button clicked opens a seperate or NEW jquery dialog window.. is this possible? 从小提琴我想要发生的是每个按钮点击打开一个单独的或新的jquery对话窗口..这可能吗?

Add a new div when click, with an ividual ID, and dialog that. 单击时添加新div,使用ividual ID和对话框。 The JSFIDDLE (when you open the second, move it, because they are overlaping each other) JSFIDDLE (当你打开第二个,移动它,因为它们相互重叠)

$('.dvclick').click(function (e) {

        var title = $(this).attr('id');
        var id = 'dialog' + '_' + title;
        $('body').append('<div id="' + id + '">This is div: ' + id + '</div>');

        $("#" + id).dialog({
            width: 200,
            title: title
        });
    });

Yes, you simply need individual window elements for each window you want to have open simultaneously. 是的,您只需要为要同时打开的每个窗口单独添加窗口元素。 Here's an updated fiddle. 这是一个更新的小提琴。

When you ask for an element to be opened as a dialog, it is styled and displayed, but showing it again can't just duplicate the element. 当您要求将一个元素作为对话框打开时,它会被设置样式并显示,但再次显示它不能只复制该元素。 You end up with the same thing being reformatted, but that effectively removes the previous dialog. 您最终会重新格式化相同的内容,但这会有效地删除以前的对话框。

To make that work well, you can link each button to a different element, like: 为了更好地工作,您可以将每个按钮链接到不同的元素,例如:

   <button id='click1' class='dvclick' data-window='info_window1'>click1</button>
<button id='click2' class='dvclick' data-window='info_window2'>click2</button>
<button id='click3' class='dvclick' data-window='info_window3'>click3</button>

and use that info in the click handler: 并在点击处理程序中使用该信息:

$('.dvclick').click(function(e) {

var title = $(this).attr('id');
var windowElem = $(this).data('window');

$("#" + windowElem).dialog({
    width: 200,
    title: title
});

}); });

Below you will find a dynamic solution. 您将在下面找到动态解决方案。 A window will be lazily-created if the button is clicked. 如果单击该按钮,将懒惰地创建一个窗口。

 $(document).ready(function () { $('.dvclick').click(function (e) { var title = $(this).attr('id'); var selector = 'info_window' + title.replace(/[^\\d]+/g, ''); // Create the window, if not already created. if ($('#' + selector).length === 0) { var newWindow = $('#info_window').clone(); newWindow.attr('id', selector); newWindow.appendTo($('body')); } $('#' + selector).dialog({ width: 200, title: title }); }); }); 
 <link href="https://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script> <!-- Dummy Window: This is a template for cloning... --> <div id="info_window" style="display:none"> <b>Info</b> <hr /> <i>Test</i> </div> <button id='click1' class='dvclick'>click1</button> <button id='click2' class='dvclick'>click2</button> <button id='click3' class='dvclick'>click3</button> 

You can separate instances to create jquery dialog, each opening in a separate window 您可以分隔实例以创建jquery对话框,每个对话框都在一个单独的窗口中打开

$('.dvclick').click(function(e) {

   var title = $(this).attr('id');

    var $window =  $("#info_window" + title);

    //check if window exist 
    if($window.attr("id") == null)
    {
       //create an dummy window and append to body
       var $div = $("<div></div>");
       $div.attr("id",info_window" + title);
       $div.css("display","none");
       $("body").append($div);
    }

    $("#info_window" + title).dialog({
      width: 200,
      title: title
    });
});

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

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