2011-11-02 117 views
1

我在嘗試從編輯Wordpress頁面時使用的排隊JavaScript文件中調用Javascript函數時出現問題。我創建了一個帶有一些AJAX超鏈接的簡單元框,我希望能夠從Javascript文件中調用函數(很簡單,但是我一直收到錯誤「blah(1)is not defined」。)WordPress的管理員 - 功能未定義?

HTML Contained in METABOX:

<a href="#" class="delete_pimg" id="pimg_1" onclick="blah(1);return false;">Delete Item</a> 

JS:

function blah(theid){ 

if (confirm("Are you sure you wish to remove this image (Note: Images are not removed from the Media Library)?")) { 

    var data = { 
    action: 'myajax-delete', 
    imgid: theid 
    }; 

    jQuery.post(ajaxurl, data, function(response) { 

     //Parse the JSON Object 
     var object = jQuery.parseJSON(response); 

     if (response.status == 'true') 
     { 
      jQuery('#file_' + theid + '_row').remove(); //remove TR 
      alert('Image removed from this portfolio'); 
     }else{ 
      alert('Sorry, that image could not removed right now, please reload the page and try again.'); 
     } 

    }); 

注:PHP服務器端代碼工作正常,完全響應預期到我的手動帖子JavaScript文件肯定是存在並通過瀏覽器下載。如預期。

如果我使用下面的代碼行,AJAX的作品(所以我知道JS是好的),但我需要能夠通過名稱調用該函數,而不是使用選擇器。我非常渴望弄清楚爲什麼我不能調用一個簡單的函數!

jQuery('.delete_pimg').click(function() { ^Above code^ } 

只是爲了重新蓋上的錯誤我得到的是單擊鏈接時:

我希望我已經清楚地說明了這「(1)沒有定義嗒嗒」 - 如果不是,請給我留言:)

+0

你給內聯和jQuery的代碼來調用函數'blah'在你的問題。既不工作?一個工程?你想使用哪一個? – mrtsherman

+0

嗨 - 我想要內聯版本的工作。每次我點擊鏈接,我都會在控制檯上看到一條消息「blah(1)沒有定義」。 – trimortis

+0

你確定你的js塊沒有語法錯誤嗎?你能從控制檯執行blah嗎? – mrtsherman

回答

0

好的基本上 - 我無法得到這個工作。我的JavaScript是絕對好的,所以爲了調用我自己的函數,我在頁面中聲明瞭它們,而不是調用JS文件。這似乎工作,我的代碼馬上執行沒有錯誤。

<script type="text/javascript">function blah(id){alert("This Works Nicely!");}</script> 

一個解決,但至少解決我的問題呢。

溼巾從額頭

0

我有同樣的問題,即blah() is not defined出汗時,發現我需要有排隊的js文件,只要定義函數,而不是用jQuery(document).ready(function($) { function blah(param){[...]} })包裹。

這裏是我的代碼看起來像現在,它把一切都爲我工作:

裏面的functions.php

(我的文件中的一小段)

function blah_init() { 
    # Want this on all pages 
    # Queue JS and CSS 
    wp_enqueue_script(
    'blah-file', // handle 
    get_template_directory_uri() . '/assets/js/blah-file.js', // source 
    array('jquery'), // registered script handles this script depends on 
    '1.0', // version 
    true // true = in footer, false (or blank) = in <head> 
); 
} 
add_action('wp_enqueue_scripts', 'blah_init'); 

的內部的blah- file.js

(這是該文件的全部內容)

//jQuery(document).ready(function($) { 
    function blah(param) { 
     console.log('Blah triggered: ', param); 
    } 
//}) 

header.php和footer內部。PHP

(一小段,我輸出的一些環節,如社會鏈接)

<!-- Ignore the href, this has nothing to do with getting this to work --> 
<a href="javascript:;" onclick="blah("facebook")">Facebook</a> 
+0

如果這個解決方案不起作用,你「可能」需要使用'add_action( 'init','blah_init');'而不是'add_action('wp_enqueue_scripts','blah_init');'或類似的東西(如果你需要在你的管理界面中使用這個功能) – skplunkerin