jQuery UI datepicker within jquery-confirm dialog

你说的曾经没有我的故事 提交于 2019-12-12 21:47:53

问题


I'm using the jquery-confirm script from the link below. It has the capability to include a form field within a dialog box. You can see this by clicking on the "Act Like a Prompt" blue button at the link below.

I've got the form (a single field) set up, but I want this input to be a datepicker, and I don't know where I should put the javascript to make this happen since this form doesn't exist until the dialog is generated.

https://craftpip.github.io/jquery-confirm/

My dialog javascript:

            $('.deal_edit').on('click', function () {
            var id = $(this).attr('data-id');
            $.confirm({
                title: 'Change end Date',
                content: 'url:form.txt',
                confirm: function () {
                    var input = this.$b.find('input#new_end_date').val();
                    var errorText = this.$b.find('.text-danger');
                    if (input.val() == '') {
                        errorText.show();
                        return false;
                    } else {
                        var data = {action: 'edit_deal', id: id, new_date: new_date};
                        $.post('ajax.php', data, function(response) {

                            $.alert({
                                title: "Updated",
                                content: "Ok, record has been updated - this page will reload.",
                                confirm: function() {
                                    location.reload();
                                }   
                            });

                        });
                    }
                }
            });
            return false;
        });     

Contents of form.txt:

<p>The only editable field currently is 'deal end date'.  (This may change soon)</p>
<div class="form-group">
  <label>New End Date</label>
  <input autofocus type="text" id="new_end_date" name="new_end_date" class="form-control">
</div>
     <p class="text-danger" style="display:none">Please enter an end date, or click 'close'.</p>

Thank you!!!


回答1:


I had the same issue, this is how I fix it.

You need to add events onOpen and onClose to the confirm dialog to add

        $.confirm({
            onOpen: function(){
                $( ".datepicker" ).datepicker();
            },
            onClose: function(){
                $(".datepicker").datepicker("destroy");
            },
            title: 'Change end Date',
            content: 'url:form.txt',
            confirm: function () {
                var input = this.$b.find('input#new_end_date').val();
                var errorText = this.$b.find('.text-danger');
                if (input.val() == '') {
                    errorText.show();
                    return false;
                } else {
                    var data = {action: 'edit_deal', id: id, new_date: new_date};
                    $.post('ajax.php', data, function(response) {

                        $.alert({
                            title: "Updated",
                            content: "Ok, record has been updated - this page will reload.",
                            confirm: function() {
                                location.reload();
                            }   
                        });

                    });
                }
            }
        });



回答2:


You can add code in the onContentReady event, like a function that is called after the plug-in create the dialog window. you can add this code to your example

 onContentReady: function () {
    $("#new_end_date").datetimepicker();
 }


来源:https://stackoverflow.com/questions/38570375/jquery-ui-datepicker-within-jquery-confirm-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!