2013-07-29 140 views
0

我正在編寫一個插件,並且該小部件包含下拉列表,並且需要根據選定的上一個下拉列表使用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

回答

1

您需要定義ajaxurl的價值是這樣的:

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

這prefferable在JavaScript的全局範圍來定義它,之前該行:

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

工作正常,謝謝! – Eric