代码之家  ›  专栏  ›  技术社区  ›  rob.m

uncaught typeerror:从wp检索标记时无法读取null的属性“length”

  •  0
  • rob.m  · 技术社区  · 6 年前

    我试图使用jquery autocomplete从 wordpress db

    首先,我在wp中设置一个函数:

    if ( ! function_exists( 'yourtheme_frontend_scripts' ) ) {
        function yourtheme_frontend_scripts() {
    
            wp_enqueue_script( 'yourtheme_custom', get_template_directory_uri().'/js/tags.js', array( 'jquery-ui-autocomplete', 'jquery' ), '1.0.0', true );
    
            wp_localize_script( 'yourtheme_custom', 'yourtheme_autocomplete', array(
                'autocomplete' => json_encode($results_array), // Results array contains all your autocomplete words
            ) );
        }
    }
    
    add_action( 'after_setup_theme', 'yourtheme_theme_setup' );
    
    if ( ! function_exists( 'yourtheme_theme_setup' ) ) {
        function yourtheme_theme_setup() {
    
            add_action( 'wp_enqueue_scripts', 'yourtheme_frontend_scripts' );
    
        }
    }
    

    然后我创建一个js:

    $(document).ready(function($) {
        "use strict";
    
        var autocomplete_terms = JSON.parse( yourtheme_autocomplete.autocomplete );
    
        var accentMap = {
            "ä": "a",
            "ö": "o",
            "Ã¥": "a",
            "č": "c"
        };
    
        var normalize = function( term ) {
            var ret = "";
            for ( var i = 0; i < term.length; i++ ) {
                ret += accentMap[ term.charAt(i) ] || term.charAt(i);
            }
            return ret;
        };
    
        $('#tags').autocomplete({
            source: function( request, response ) {
                var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
                response( $.grep( autocomplete_terms, function( value ) {
                    value = value.label || value.value || value;
                    return matcher.test( value ) || matcher.test( normalize( value ) );
                }) );
            }
        });
    
    });
    

    最后是我的意见:

    <input class="form-control" id="tags">
    

    但我一开始打字就发现:

    uncaught typeerror:无法读取null的属性“length”

    在a.(示例路径/匿名函数)。(匿名函数)。源

    2 回复  |  直到 6 年前
        1
  •  0
  •   Rahul Sharma    6 年前

    如果您使用的是divi,那么可能是文件链接的问题。链接已选择为操作设置的每个文件。

        2
  •  0
  •   rob.m    6 年前

    我就是这样解决的:

    在函数php中,我运行ajax循环我的类别列表,并检查用户键入的内容是否存在,否则我让他自己编写:

      add_action( 'wp_footer', 'ajax_fetch' );
      function ajax_fetch() {
    
    ?>
    
      <script type="text/javascript">
      function fetch(){
    
          jQuery.ajax({
              url: '<?php echo admin_url('admin-ajax.php'); ?>',
              type: 'post',
              data: { action: 'data_fetch', keyword: jQuery('#keyword').val() },
              success: function(data) {
                    var dataArray = data.split("|");
                     jQuery( "#keyword" ).autocomplete({
                      source: dataArray
                    });
              }
          });
    
      }
      </script>
    
    <?php
      }
    
    add_action('wp_ajax_data_fetch' , 'data_fetch');
    add_action('wp_ajax_nopriv_data_fetch','data_fetch');
    
    function data_fetch(){
        $key = $_POST['keyword'];
        $args = array(
          'orderby' => 'id',
          'hide_empty'=> 0,
          'name__like' => $key
      );
      $categories = get_categories($args);
      foreach ($categories as $cat) {
        echo $cat->name."|";
      }
      die();
    }
    

    然后我使用jquery autocomplete设置一个输入:

    <div class="ui-widget">
      <label for="keyword">Tags: </label>
      <input id="keyword"  onkeyup="fetch()">
    </div>
    

    注:

    我们需要在wp函数中加载autocomplete和css:

    function add_scripts(){
      wp_enqueue_script( 'jquery-ui-autocomplete' );
    }
    add_action('wp_enqueue_scripts', 'add_scripts');
    
    function add_stylesheet_to_head() {
      echo "<link href='https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.css' rel='stylesheet' type='text/css'>";
    }