0

我第一次尝试使用 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" );
      });
  });
    });

})

但我收到这样的错误:

Uncaught TypeError: Object [object Object] has no method 'dialog' 。

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

谢谢。

4

4 回答 4

0

我认为 jQuery 已经加载。还有 wp_enqueue_script( $this->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/

于 2013-07-31T09:40:07.313 回答
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/

如果您下载了完整的库,它将被包括在内。

于 2013-07-31T09:42:26.550 回答
0

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

  • 1 -$this->plugin_slug .'-admin-styles'所有 3 个文件上的 " " 告诉 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' ) );
}
于 2013-07-31T10:22:29.950 回答
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__ ) );
}
于 2013-07-31T09:38:37.400 回答