Wordpress管理-函数未定义


Wordpress Admin - Function not defined?

我在编辑Wordpress页面时尝试从排队Javascript文件调用Javascript函数时遇到了问题。我已经创建了一个简单的元框与一些AJAX超链接,我希望能够从Javascript文件调用函数(相当简单的东西,但我一直得到错误"blah(1)是未定义的"。

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服务器端代码工作良好,绝对响应我的手动帖子。

如果我使用下面的代码行,AJAX工作(所以我知道JS是OK的),但我需要能够按名称调用函数,而不是使用选择器。我很想知道为什么我不能调用一个简单的函数!!!!

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

只是为了重新定义当链接被点击时我得到的错误:'blah(1) is not defined'

我希望我已经解释清楚了——如果没有,请告诉我:)

Ok基本上-我无法让这个工作。我的javascript绝对没问题,所以为了调用我自己的函数,我在页面本身声明了它们,而不是在JS文件中调用。这似乎工作和我的代码执行没有错误直接。

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

一个工作,但至少解决了我的问题。

擦去额头上的汗

我有同样的问题,其中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>