2012-02-29 71 views
1

我正在編寫一個jquery插件,並且我需要一些按鈕來擁有雙重狀態(如編輯/保存) 我通過JSON獲取此信息並將其插入到按鈕中:jquery中的數據對象在replaceWith後是undefined

node 
    - current //['primary'/'secondary'] 
    - primary // some info 
    - secondary // some info 

一旦我點擊按鈕,我到這裏來改變行動。所以我想通過模板和我從button.data獲得的信息來替換整個鏈接。 因爲我想不僅替換innerHtml而且替換外部,我必須使用'替換替換'。然後,我將'數據'複製到新按鈕,並且(理想情況下)刪除較舊的按鈕。

changeButtonAction : function(button, selector){ 
     var node = button.data('node'), 
      info; 

     if(node.current == 'primary'){ 
      info = node.secondary; 
      node.current = 'secondary'; 
     }else{ 
      info = node.primary; 
      node.current = 'primary'; 
     } 

     button.replaceWith(multiReplace(OPERATION_ITEM, info, true)); 
     button.data('node', $.extend(true, {}, node)); 

     ... //bit of interaction 
} 

的事情是:一旦出了功能我鬆了新的數據,因爲它說,這是不確定。 有人可以幫忙嗎?使用'replaceWith'並不是必須的,所以如果你想出另一個解決方案,它會好的。

+0

如果可能的話請在HTTP示範://的jsfiddle .net /以便其他人可以更好地瞭解您的問題。不需要複製所有代碼,只需要複製問題所在的部分。使用左側的「添加資源」添加其他js文件。 – Diode 2012-02-29 20:01:51

回答

0

好的,我解決了它。

感謝二極管我試着在jsfiddle中重現它。點擊功能也不起作用,所以我改變了一下我的代碼。相反,文本替換的:

button.replaceWith(multiReplace(OPERATION_ITEM, info, true)); 
button.data('node', $.extend(true, {}, node)); 

與對象就做:

var button2 = $(multiReplace(OPERATION_ITEM, info, true)) 
    .data('node', $.extend(true, {}, node)); 
button.replaceWith(button2); 

你可以看到它在行動: http://jsfiddle.net/p8vMR/9/