2016-07-14 81 views
0

我試圖在使用PHP腳本的wordpress網站上設置自動完成。但是目前我的代碼中沒有顯示任何內容。我明白,一般的想法是有一個jQuery函數,它會使用PHP腳本來提取MySQL數據(suggest.php)。另外如果我要把WordPress中的jQuery自動完成

<script> 
    $(function() { 
    $("#tags").autocomplete({ 
     source: 'suggest.php', 
     minLength:1 

    }); 
    }); 
</script> 

在myScript.js下的js文件夾下,我將如何訪問它?我的完整代碼如下...

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"> 
<link rel="stylesheet" href="/resources/demos/style.css"> 
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> 
<script> 
    $(function() { 
    $("#tags").autocomplete({ 
     source: 'suggest.php', 
     minLength:1 

    }); 
    }); 
</script> 

<form action="" method="post"> 
Name: <input type="text" name="tags" id="tags" value="<?php echo isset($_POST['tags']) ? $_POST['tags'] : '' ?>"/> 
</form> 

回答

0

首先,不包括整個jquery-ui,這是完全沒有必要的。

其次,不要手動放置腳本。這就是爲什麼WordPress有enqueuing

首先,您需要將腳本排入自動完成的位置,並且必須依賴於自動完成。因此,在您functions.php你把

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'); 

    } 
} 

if (! function_exists('yourtheme_frontend_scripts')) { 
    function yourtheme_frontend_scripts() { 

     wp_enqueue_script('yourtheme_custom', get_template_directory_uri().'/js/custom.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 
     )); 
    } 
} 

你怎麼打電話給你.js文件是由你。我把它稱爲custom.js,並把它放在主題中的/ js文件夾中。

您還需要一個$results_array,其中包含您的所有自動完成單詞。我通常手動把它們,或者如果需要查詢數據庫。

然後在你的custom.js你會把這樣的事情:

jQuery(document).ready(function($) { 
    "use strict"; 

    var autocomplete_terms = JSON.parse(yourtheme_autocomplete.autocomplete); 


    $('#tags').autocomplete({ 
     source: autocomplete_terms, 
     minLength: 1 
    }); 

}); 

應該工作的罰款。一個很酷的補充是,如果你有口音或拉丁擴展術語是將它們添加到口音地圖

jQuery(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)); 
      })); 
     } 
    }); 

}); 

希望這會有所幫助。

+0

因此,根據這個解決方案,我們將設置函數,它將利用MySQL查詢文件中的results_array作爲functions.php中的suggest.php並設置custom.js? 我怎麼enque custom.js在PHP頁面? 這個參數對於 的參數「wp_enqueue_script案件? – findataguy

+0

我不確定你的'suggest.php'文件的內容是什麼,但'$ results_array'需要是一個數組。無論您是手動編寫該數組,還是創建將以數組形式返回結果的查詢都不重要。讓數組傳遞給JavaScript文件非常重要。 enqueue部分在我的代碼中給出。將它粘貼到你的'functions.php'中,你將有'custom.js'入隊(確保它位於指定位置)。我忘了用'get_template_directory_uri()'函數替換'TEMPPATH'常量。我現在要修復它 –

+0

所以基本上我覺得有了基本的想法並設置了自動完成。但我有兩個問題。其中一個我無法從'suggest.php'獲得源代碼,其中'json-encoded'列表將被'echo'。如果我要手動輸入數組,自動完成列表不會在第一次輸入時顯示,但如果我點擊提交按鈕,它就會顯示出來。基本上我的custom.js文件看起來像這樣 – findataguy