2014-12-09 88 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