我在嘗試從編輯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)沒有定義嗒嗒」 - 如果不是,請給我留言:)
你給內聯和jQuery的代碼來調用函數'blah'在你的問題。既不工作?一個工程?你想使用哪一個? – mrtsherman
嗨 - 我想要內聯版本的工作。每次我點擊鏈接,我都會在控制檯上看到一條消息「blah(1)沒有定義」。 – trimortis
你確定你的js塊沒有語法錯誤嗎?你能從控制檯執行blah嗎? – mrtsherman