2013-02-27 67 views
0

我有一個工作的jquery功能,我可以通過PHP這樣調用無法調用基於參數的jQuery基函數?

if (!order) 

     { echo '<script> 
$(function() { 
$this = $("#ui-accordion-accordion-panel-0"); 
// parent tab 
var parent = $("#accordion").parent(); 
//parent.adderror(); 

// the content 
$this.adderror(); 

// the header 
var header = $this.attr("aria-labelledby"); 
$("#"+header).adderror(); 


// the tab 
var tab = parent.attr("aria-labelledby"); 
$("#"+tab).parent().adderror(); 
}); 

    </script>';} 

我想使這個全局函數(在.js文件中甚至更好,如果定義外),我可以在任何地方撥打我想在我的PHP,這需要面板作爲參數。

所以我基本上會調用show_error(「#ui-accordion-accordion-panel-0」)。我怎麼能這樣做?當我嘗試定義函數,然後調用它時 - 沒有任何變化。通過使用這裏定義無濟於事techinques爲(var functionName = function() {} vs function functionName() {}

在此先感謝

好吧,我現在有一個error.js文件,我已經恰當地聯動()

function show_error(selector) { 
var $this = $(selector); 

// parent tab 
var parent = $("#accordion").parent(); 

//parent.adderror(); 

// the content 
$this.adderror(); 

// the header 
var header = $this.attr("aria-labelledby"); 
$("#"+header).adderror(); 


// the tab 
var tab = parent.attr("aria-labelledby"); 
$("#"+tab).parent().adderror(); 
} 
+0

FWIW,你的函數的獵物下降到[*隱全局*的恐怖(http://blog.niftysnippets.org/2008/03/horror-的隱-globals.html),因爲你沒有聲明你的'$ this'變量。 – 2013-02-27 11:37:26

+0

行中的'parent' var tab = parent.attr(「aria-labelledby」);'?你沒有顯示任何東西顯示你在哪裏得到。 – 2013-02-27 11:38:53

+0

我的不好,以爲我只是張貼片段。結束了你所有的困惑,我很抱歉。剛剛做了相關的編輯。 – 2013-02-27 11:43:06

回答

1

功能在外部.js文件應該是這樣的:

function show_error(selector) { 
    var $this = $(selector); 
    // parent tab 
    var parent = $("#accordion").parent(); 
    //parent.adderror(); 

    // the content 
    $this.adderror(); 

    // the header 
    var header = $this.attr("aria-labelledby"); 
    $("#"+header).adderror(); 

    // the tab 
    var tab = parent.attr("aria-labelledby"); 
    $("#"+tab).parent().adderror(); 
} 
+0

我嘗試了一些非常相似的東西,沒有工作。現在再試一次。 – 2013-02-27 11:37:25

+0

@PraktikBothra:呃,再一次,你會得到'父母'的問題。但從根本上說,**能夠**,除非你的問題沒有顯示你網頁上的其他問題。 – 2013-02-27 11:38:06

+0

@PratikBothra:更新了代碼更新。 – 2013-02-27 11:47:21