2014-12-09 36 views
-1

我有一個插件,它包含一個插入/從數據庫中獲取數據的函數。 我的問題是我如何使用jquery或鏈接在wordpress插件中的任何其他JS。 我搜索並找到很多文章,但它不清楚,請我nead步驟添加jquery文件在我的插件。 我試過的貝爾臺階 在我的頁面頂部添加此行。在wordpress插件中使用jquery

其是我的插件代碼

和下面是位於C fn.js:\瓦帕\萬維網\測試\可溼性粉劑內容\插件\爲myplugin \ fn.js

<script type="text/javascript"> 
$(document).ready(function(){ 
    <!-- ajax method to bind the dropdown menu of model--> 
     $("#select_brand").change(function(){ 
     var id=$(this).val(); 
     var dataString = 'id='+ id; 
     $.ajax({ 
     type: "POST", 
     url: "model.php", 
     data: dataString, 
     cache: false, 
     success: function(data) 
     { 
      $("#select_Model").html(data); 
     } 
     }); 
     }); 

    <!-- ajax method to bind the dropdown menu of city--> 
     $("#select_Country").change(function(){ 
     var id=$(this).val(); 
     var dataString = 'id='+ id; 
     $.ajax({ 
     type: "POST", 
     url: "city.php", 
     data: dataString, 
     cache: false, 
     success: function(data) 
     { 
      alert("yes"); 
      $("#select_city").html(data); 
     } 
     }); 
     }); 
     }); 
</script> 

和city.php

<?php 
    if($_POST['id']) 
    { 
     global $wpdb; 
     $id=$_POST['id']; 
     $query="select id,CountryCode,City from _city where CountryCode='$id'"; 
     $result=$wpdb->get_results($query); 
     echo '<option selected="selected" disabled="disabled"> -- Select City -- </option>'; 
     foreach ($result as $row) 
     { 
      $id=$row->id; 
      $city=$row->city; 
      echo '<option value="'.$id.'">'.$city.'</option>'; 
     } 
    } 
?> 

問題的最終代碼城市下拉不能得到的值。

+0

這聽起來像正在排隊的jQuery就好了。你在哪裏看到錯誤? – rnevius 2014-12-09 12:38:40

+0

當我在控制檯瀏覽器中按Ctrl + Shift + J。 和代碼不工作 – Bassem 2014-12-09 12:40:31

+0

@webeno,我已經閱讀但有些不明確 – Bassem 2014-12-09 12:41:44

回答

1

通過在你的插件文件中使用

wp_enqueue_script($handle, $src, $deps, $ver, $in_footer); 

要排隊腳本

示例

function my_scripts_method() { 
wp_enqueue_script(
    'jsscript', 
    plugins_url('/js/jsscript.js' , __FILE__), 
    array('jquery') 
); 
} 

add_action('wp_enqueue_scripts', 'my_scripts_method'); 

鏈路http://codex.wordpress.org/Function_Reference/wp_enqueue_script

+0

也不是工作,我會發布我的代碼,請檢查它,告訴我爲什麼它不工作。 – Bassem 2014-12-09 13:21:57

+0

請現在檢查 – Bassem 2014-12-09 13:35:24

+0

嘿PLZ入隊my_scripts_method()函數form_creation()函數外我會做的很好 – 2014-12-09 14:20:48

2

你很明顯在wordpress實現中的其他地方包含了另一個自定義腳本/ js文件,但不會依賴於jQuery,因此它在jQuery被加載之前運行。所以要確保你的包含jQuery代碼的自定義腳本是依賴它的。

見下文(使函數內注意array('jquery')):

<?php 

function my_scripts_method() { 
    wp_enqueue_script(
     'custom-script', 
     get_stylesheet_directory_uri() . '/js/custom_script.js', 
     array('jquery') 
    ); 
} 

add_action('wp_enqueue_scripts', 'my_scripts_method'); 

?> 

來源:Function Reference/wp enqueue script - Link a Theme Script Which Depends on jQuery

+0

也不行,我會發布我的代碼,請檢查一下,告訴我爲什麼它不起作用 – Bassem 2014-12-09 13:22:44

+0

請檢查上面的代碼 – Bassem 2014-12-09 13:35:42