Jquery UI对话框

时间:2013-07-31 09:30:49

标签: jquery wordpress jquery-ui

我第一次尝试使用jQuery UI。在这里,我展示了我的代码:

(在wordpress索引文件中)

public function enqueue_admin_scripts() 
    {
        wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-1.9.1.js', __FILE__ ) );
        wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/admin.js', __FILE__ ) );
        wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-ui-1.10.3.custom.min.js', __FILE__ ) );

    }

这是我的admin.js

jQuery(document).ready(function(){

    jQuery("#add_new_gallery").click(function(){

        // alert (456);

    jQuery(function() {
      var name = jQuery( "#name" ),
      email = jQuery( "#email" ),
      password = jQuery( "#password" ),
      allFields = jQuery( [] ).add( name ).add( email ).add( password ),
      tips = jQuery( ".validateTips" );

    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }

    function checkLength( o, n, min, max ) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
          min + " and " + max + "." );
        return false;
      } else {
        return true;
      }
    }

    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }

    jQuery( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": function() {
          var bValid = true;
          allFields.removeClass( "ui-state-error" );

          bValid = bValid && checkLength( name, "username", 3, 16 );
          bValid = bValid && checkLength( email, "email", 6, 80 );
          bValid = bValid && checkLength( password, "password", 5, 16 );

          bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+jQuery/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
          // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
          bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\jQuery%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\jQuery%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?jQuery/i, "eg. ui@jquery.com" );
          bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+jQuery/, "Password field only allow : a-z 0-9" );

          if ( bValid ) {
            jQuery( "#users tbody" ).append( "<tr>" +
              "<td>" + name.val() + "</td>" +
              "<td>" + email.val() + "</td>" +
              "<td>" + password.val() + "</td>" +
            "</tr>" );
            jQuery( this ).dialog( "close" );
          }
        },
        Cancel: function() {
          jQuery( this ).dialog( "close" );
        }
      },
      close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
      }
    });

    jQuery( "#create-user" )
      .button()
      .click(function() {
        jQuery( "#dialog-form" ).dialog( "open" );
      });
  });
    });

})

但是我收到了这样的错误:

  

未捕获的TypeError:对象[object Object]没有方法'对话'。

可能是什么问题?怎么解决这个问题?

感谢。

4 个答案:

答案 0 :(得分:0)

您需要重新排序插件,因为admin.js依赖于jquery UI:

public function enqueue_admin_scripts() 
{
    wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-1.9.1.js', __FILE__ ) );
    wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-ui-1.10.3.custom.min.js', __FILE__ ) );
    wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/admin.js', __FILE__ ) );
}

答案 1 :(得分:0)

我认为jQuery已经加载了。和wp_enqueue_script($ this-&gt; plugin_slug .'- admin-styles',plugins_url('js / jquery-1.9.1.js', FILE ));与jQuery对象冲突。请检查http://css-tricks.com/snippets/wordpress/include-jquery-in-wordpress-theme/

答案 2 :(得分:0)

您需要重新排序您的javascript包括:

public function enqueue_admin_scripts() 
{
    wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-1.9.1.js', __FILE__ ) );
    wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-ui-1.10.3.custom.min.js', __FILE__ ) );
    wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/admin.js', __FILE__ ) );
}

您需要确保拥有包含对话框的jQuery UI构建: http://jqueryui.com/download/

如果您下载了完整的库,则会将其包含在内。

答案 3 :(得分:0)

除了重新排序包含和明显的答案,jquery已经包含在wordpress管理员中,我还看到了2个问题。

  • 1 - 所有3个文件中的“$this->plugin_slug .'-admin-styles'”告诉wordpress实际上只包含一个文件,您需要定义唯一的句柄名称,而不是覆盖以前的文件。
  • 2 - 您可能还需要考虑定义依赖项,如:wp_enqueue_script( $this->plugin_slug .'-admin-styles', plugins_url( 'js/jquery-1.9.1.js', __FILE__ ), array('jquery') );其中jquery是您为jquery文件提供的唯一句柄。

总之,你应该:

public function enqueue_admin_scripts() 
{
    wp_enqueue_script( $this->plugin_slug .'-admin-styles-new-jquery', plugins_url( 'js/jquery-1.9.1.js', __FILE__ ) );
    wp_enqueue_script( $this->plugin_slug .'-admin-styles-new-jquery-ui', plugins_url( 'js/jquery-ui-1.10.3.custom.min.js', __FILE__ ), array( $this->plugin_slug .'-admin-styles-new-jquery' ) );
    wp_enqueue_script( $this->plugin_slug .'-admin-styles-my-admin-javascript', plugins_url( 'js/admin.js', __FILE__ ), array( $this->plugin_slug .'-admin-styles-new-jquery' ) );
}