如何在动态创建的jQuery UI对话框中使用datepicker?

时间:2015-05-31 22:51:42

标签: javascript jquery jquery-ui datepicker dialog

我无法在动态创建的jQuery UI对话框中激活datepicker():

的index.html

$(document).ready(function() {

    var $loading = $('<img src="./images/loading.gif" alt="loading">');

    $('.page-popup').each(function() {

        var $dialog = $('<div></div>')
                .append($loading.clone());
        var $link = $(this).one('click', function() {
                $dialog
                        .load($link.attr('href'))
                        .dialog({
                                title: $link.attr('title'),
                                width: 600,
                                height: 300
                        });

                $link.click(function() {                
                        $dialog.dialog('open');

                        return false;
                });

                return false;
        });

    });

    $( ".datepicker" ).datepicker({         
            dateFormat: "yy-mm-dd"      
    });

});

通过类似链接加载的外部页面:

<a href="input.html" title="Input" class="page-popup">Input</a>

它只有一个表格来选择或更正日期:

input.html

<form method="post" action="?">
    <input type="text" name="date" value="2000-01-01" class="datepicker">
    <input type="submit">
</form>

如何激活不同对话框的日期选择器?

2 个答案:

答案 0 :(得分:0)

在对话框的open事件中渲染日期选择器,如下所示。

                   $dialog
                    .load($link.attr('href'))
                    .dialog({
                            title: $link.attr('title'),
                            width: 600,
                            height: 300,
                            open: function(){
                             $( ".datepicker" ).datepicker({         
                                     dateFormat: "yy-mm-dd"      
                                  });
                             }
                    });

答案 1 :(得分:0)

问题是子页面无法重新加载jquery.js和jquery-ui.js。所以这是我的解决方案:

<强>的index.html

<html>
    <head>
    <script type="text/javascript" src="./js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="./js/jquery-ui.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {              
        var $loading = $('<img src="./images/loading.gif" alt="loading">');        
        $('.page-popup').each(function() {                    
            var $dialog = $('<div></div>').append($loading.clone());
            var $link = $(this).one('click', function() {
                    $dialog
                            .load($link.attr('href'))
                            .dialog({
                                    title: $link.attr('title'),
                                    width: 600,
                                    height: 300
                            });        
                    $link.click(function() {                
                            $dialog.dialog('open');        
                            return false;
                    });        
                    return false;
            });                
        });            
        $( ".datepicker" ).datepicker({         
                dateFormat: "yy-mm-dd"      
        });            
    });
    </script>
  </head>
<body>

   <a href="input.html" title="Input" class="page-popup">Input</a>

</body>
</html>

<强> input.html

<html>
    <head>
    <!-- Don't load jquery and jquery-ui again!!! -->
    <script type="text/javascript">
       $( ".datepicker" ).datepicker({            
            dateFormat: "yy-mm-dd"      
       });
    </script>
</head>
<body>

    <form method="post" action="?">
        <input type="text" name="date" value="2000-01-01" class="datepicker">
        <input type="submit">
    </form>

</body>
</html>