【问题标题】:find a element in jQuery dialog after it opens打开后在 jQuery 对话框中找到一个元素
【发布时间】:2014-07-19 17:37:27
【问题描述】:

我正在动态创建一个对话框。我接下来要做的是将一些输入与.datepicker() 绑定,但我似乎根本找不到输入。我在这里错过了什么?

JSFIDDLE

function createDialog(top, dialog_width, data) {
        $('<div></div>')
            .addClass('add-dialog')
            .appendTo('body')
            .dialog({
                title: 'test',
                closeText: '<i class="fa fa-times"></i>',
                close: function () {
                    $(this).remove();
                },
                open: function (event, ui) {
                    //has the full html with the inputs
                    console.log(this);

                    //trying to get them
                    var inputs = $(this).find('input');
                    console.log(inputs.length);

                    //also tried this, no luck
                    //$(this).parent().promise().done(function() {
                    //    var t = $(this).find('input');
                    //});

                },
                modal: true,
                width: dialog_width,
                position: ['center', (top - 12)],
                draggable: false,
                resizable: false
            }).html(data);
    }

【问题讨论】:

  • 在打开对话框回调之前附加 html(data),你没有得到元素,因为 add-dialog div 中没有 html
  • @ramby,确实如此。 console.log(this); 显示完整的 html
  • 在下面查看我的答案

标签: javascript jquery html jquery-ui jquery-dialog


【解决方案1】:

只需更改初始化顺序....添加html,然后初始化对话框

$('<div></div>').html(data).dialog({ /*opts*/})

您在对话框初始化后添加 html,因此 open 事件已经发生并且还没有找到任何元素

DEMO

【讨论】:

  • 我无法让您的示例在控制台中记录任何内容?
  • 在演示中对我来说很好。你是用ajax加载的吗?
【解决方案2】:

您可以为所需的每个元素指定一个类,例如:

<input type='text' class='needIt'/>

然后使用:

$(this).find('.needIt')

【讨论】:

    【解决方案3】:

    您需要在 jquery 对象插入 DOM 后保存对它的引用,然后获取对 jquery UI 对话框实例的引用。这个实例有很多有用的属性。示例:

    $('form').append($body); // I've appended dialog as last element inside form in my document, $body is markup, received from AJAX
    
    $body.dialog({...}); // I've turned markup into jquery UI dialog
    
    var dialogInstance = $body.dialog('instance'); // I receive reference to jquery UI dialog instance in any place, for instance, button click event
    
    dialogInstance.uiDialogButtonPane.hide(); // I can access button pane in the dialog and many other objects, like caption area, etc.
    

    【讨论】:

      【解决方案4】:

      尝试在 appendTo() 之后添加 html

      function createDialog(top, dialog_width, data) {
              $('<div></div>')
                  .addClass('add-dialog')
                  .html(data)// added html here
                  .appendTo('body')
                  .dialog({
                      title: 'test',
                      closeText: '<i class="fa fa-times"></i>',
                      close: function () {
                          $(this).remove();
                      },
                      open: function (event, ui) {
                          //has the full html with the inputs
                          console.log(this);
      
                          //trying to get them
                          var inputs = $(this).find('input');
                          console.log(inputs.length);
      
                          //also tried this, no luck
                          //$(this).parent().promise().done(function() {
                          //    var t = $(this).find('input');
                          //});
      
                      },
                      modal: true,
                      width: dialog_width,
                      position: ['center', (top - 12)],
                      draggable: false,
                      resizable: false
                  });
          }
      

      【讨论】:

      • dialog 自动附加...如果需要,原始演示将如何工作?
      猜你喜欢
      • 2012-09-03
      • 2018-03-23
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      相关资源
      最近更新 更多