WordPress - AJAX代码不会触发操作

问题描述:

我正在编写一个插件,并且该小部件包含下拉列表,并且需要根据选定的上一个下拉列表使用AJAX从数据库中检索每个连续的下拉列表的值值。WordPress - AJAX代码不会触发操作

我要关闭从WordPress抄本的代码稍加修改的版本在这里找到:http://codex.wordpress.org/AJAX_in_Plugins

出于某种原因,动作功能要么没有被所谓的或有在我的代码一些错误:

// add action hooks 

add_action('wp_footer', 'er_qm_ajax_handler'); 
add_action('wp_ajax_repopulate_widget_club_type', 'repopulate_widget_club_type'); 
add_action('wp_ajax_nopriv_repopulate_widget_club_type', 'repopulate_widget_club_type'); 

// action for the wp_footer hook to insert the javascript 

function er_qm_ajax_handler(){ 
    ?> 
     <script type="text/javascript" >  
     jQuery(document).ready(function($) { 
      jQuery('#widget_manufacturer').val('3').change(function(){ 
       var manufacturer = $("#widget_manufacturer").val(); 
       var data = { 
        action: 'repopulate_widget_club_type', 
        selected_manufacturer: manufacturer 
       }; 
       // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php 
       jQuery.post(ajaxurl, data, function(response) { 
        alert('Got this from the server: ' + response); 
       }); 
      }); 
     }); 
     </script> 
    <?php 
} 

// The action function 

function repopulate_widget_club_type(){ 
    die("Got to the action"); 
} 

不完全知道我在做什么错了,因为我没有得到来自AJAX后的反应,我知道了jQuery .change()正在为我设置了一些警告()的测试一下一旦我改变了下拉列表的值。

任何想法和帮助将不胜感激,谢谢!

+1

'jQuery的(文件)。就绪(函数($){'允许你安全地在其范围内使用'$'而不是'jQuery'。 –

+0

什么是'ajaxurl'值? – brasofilo

+0

布拉德 - 感谢我在想这个,Brasofil-我想知道是否可能有某种东西在Codex中它提到了这一点包含链接到管理员标题中的admin-ajax.php,但可能不在客户端? – Eric

您需要定义ajaxurl的价值是这样的:

var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>' 

这prefferable在JavaScript的全局范围来定义它,之前该行:

jQuery(document).ready(function($) { 
+0

工作正常,谢谢! – Eric