簡體   English   中英

Wordpress Admin-功能未定義?

[英]Wordpress Admin - Function not defined?

我在嘗試從編輯Wordpress頁面時使用的排隊javascript文件中調用Javascript函數時遇到問題。 我用一些AJAX超鏈接創建了一個簡單的meta框,希望能夠從Javascript文件中調用函數(很簡單,但是我一直收到錯誤消息“ blah(1)not defined”)。

METABOX中包含的HTML:

<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^ } 

只是為了重新概括單擊鏈接時出現的錯誤: “未定義blah(1)”

我希望我已經清楚地說明了這一點-如果沒有,請給我大聲喊叫:)

我遇到了blah() is not defined的相同問題,發現我需要讓排隊的js文件僅定義函數,而不是用jQuery(document).ready(function($) { function blah(param){[...]} })包裝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>

好的,基本上-我無法使它正常工作。 我的JavaScript絕對不錯,因此為了調用自己的函數,我在頁面本身內聲明了它們,而不是在JS文件中進行了聲明。 這似乎可行,並且我的代碼立即執行,沒有錯誤。

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

解決問題,但至少解決了我的問題。

擦拭額頭上的汗水

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM