2012-11-30 43 views
3

使用JQ和JQM開發phonegap應用程序,這個奇怪的問題真的讓我很難過。我真的可以使用我能得到的所有幫助,謝謝大家!。事件綁定中的數據()

的硬類解釋,但只是無法得到。數據()在2個動態綁定的事件處理程序的工作同樣在這2個事件處理程序看起來幾乎幾乎相同。我必須錯過了一些地方..唉..

HTML

  <ul data-role="listview" id="notelist" data-split-icon="minus" data-split-theme="c"> 
       <li id="entrytemplate" style="display:none"> 
        <a class="btnpopupdetails" href="#" data-position-to="window" data-rel="popup" data-transition="pop"> 
         <h3>TEMPLATE Faulty lift</h3> 
         <p>TEMPLATE Lift A1, at lobby, Skyscraper Plaza, was reported broken down on 25th Dec 2012</p> 
        </a> 
        <a class="btndelete" href="#" data-position-to="window" data-rel="popup" data-transition="pop">Delete</a> 
       </li> 
      </ul> 

JS

// row is a single row from a resultset of a successful sql query 

newrow.data('rowid', row.id); // integer 

newrow.data('rowtitle', row.txttitle); // text 

newrow.data('rowdescription', row.txtdescription); // text 

console.log(newrow.data('rowtitle')); // value retrieved and displayed fine! 

console.log(newrow.data('rowdescription')); // value retrieved and displayed fine! 

newrow.appendTo('#notelist'); 

newrow.find('h3').text(row.txttitle); 

newrow.find('p').text(row.txtdescription); 


newrow.find('.btnpopupdetails').click(function() { 

    selectedrow = $(this).parent(); 

    selectedrowid = selectedrow.data('rowid'); 

    selectedrowtitle = selectedrow.data('rowtitle'); 

    selectedrowdscription = selectedrow.data('rowdescription'); 

    console.log(selectedrow.data('rowid')); // "TypeError: 'undefined' is not an object" 

    console.log(selectedrow.data('rowtitle')); // "TypeError: 'undefined' is not an object" 

    console.log(selectedrow.data('rowdescription')); // "TypeError: 'undefined' is not an object" 
}); 


newrow.find('.btndelete').click(function() { 

    selectedrow = $(this).parent(); 

    selectedrowid = selectedrow.data('rowid'); 

    selectedrowtitle = selectedrow.data('rowtitle'); 

    selectedrowdscription = selectedrow.data('rowdescription'); 

    console.log(selectedrow.data('rowid')); // value retrieved and displayed fine! 

    console.log(selectedrow.data('rowtitle')); // value retrieved and displayed fine! 

    console.log(selectedrow.data('rowdescription')); // value retrieved and displayed fine! 
+0

如果沒有可用的例子也很難指出來..你能創建一個小提琴??。 –

+0

感謝您的幫助和編輯,夥計們,真的很感激! :-) –

回答

0

我有一個想法,什麼可能的:,可以去JQM增強時鏈接<a>的標記在<li>中,它實際上將其封裝在2個div中。在頁面加載後檢查您的標記以確認這一點。如果我將你的標記加載到<li>,第一個<a>出現在2格中,第二個是<li>的直接子,所以這可以解釋你所看到的行爲。

這意味着當您指示selectedrow = $(this).parent();我懷疑它表示包含您的<a class="btnpopupdetails">而不是您附加數據的<li>的div;

而是嘗試用這種替換該行:selectedrow = $(this).closest('li');或給你<li>一個新的類,並使用$(this).closest('.newClass')

把那一個鏡頭,讓我知道如何工作了。應該是這樣的(這兩個元素右1處理程序):

newrow.find('.btnpopupdetails, .btndelete').click(function() { 
    selectedrow = $(this).closest('li'); // THIS HAS CHANGED! 
    selectedrowid = selectedrow.data('rowid'); 
    selectedrowtitle = selectedrow.data('rowtitle'); 
    selectedrowdscription = selectedrow.data('rowdescription'); 
    console.log(selectedrow.data('rowid')); // "TypeError: 'undefined' is not an object" 
    console.log(selectedrow.data('rowtitle')); // "TypeError: 'undefined' is not an object" 
    console.log(selectedrow.data('rowdescription')); // "TypeError: 'undefined' is not an object" 
}); 
+0

是的.. $(this).closest('li')釘了它!非常感謝你的幫助!可能需要重新訪問一些參考文獻,並檢查一些JQM在引擎蓋操作下不太明顯。:-) –

相關問題