jquerymobile之collapsible可折疊塊標題內容動態顯示


jquery mobile提供了一種可折疊的組件--data-role="collapsible",這種組件可以通過點擊折疊塊頭部來展開/折疊塊內的內容,詳細組件說明可參考w3cshool可折疊塊

 

在做一個小項目的時候,我有一個模塊用到了可折疊塊這個組件:初始頁面時collapsible的標題是空的,只有用戶在collapsible下面的文本框輸入內容后,標題會跟文本框內容同步。

 

如下圖所示,初始界面時,collapsible標題為空,只有在“險種”文本框中輸入內容,collapsible標題才會產生變化。

 

對應的HTML代碼如下:

 

Html代碼   收藏代碼
  1. <div data-role="collapsible" id="collapsible">  
  2.             <h3 class="h3">&nbsp</h3>  
  3.             <div class="ui-field-contain" data-controltype="textinput">  
  4.             <label for="insurance_name">  
  5.                 險種  
  6.             </label>  
  7.             <input name="insurance_name" value="" type="text">  
  8.             </div>  
  9.               
  10.         <div class="ui-field-contain" data-controltype="textinput">  
  11.             <label for="insurance_company">  
  12.                 投保公司  
  13.             </label>  
  14.             <input name="insurance_company" value="" type="text">  
  15.         </div>  
  16.         <div class="ui-field-contain" data-controltype="dateinput">  
  17.             <label for="insurance_date">  
  18.                 投保日期  
  19.             </label>  
  20.             <input name="insurance_date" value="" type="date">  
  21.         </div>  
  22.         <div class="ui-field-contain" data-controltype="textinput">  
  23.             <label for="insurance_year">  
  24.                 年期  
  25.             </label>  
  26.             <input name="year_period" value="" type="text">  
  27.         </div>  
  28.         <div class="ui-field-contain" data-controltype="textinput">  
  29.             <label for="insurance_cost">  
  30.                 年保費  
  31.             </label>  
  32.             <input name="year_cost" value="" type="text">  
  33.         </div>  
  34.         <div class="ui-field-contain" data-controltype="textarea">  
  35.             <label for="insurance_comment">  
  36.                 備注  
  37.             </label>  
  38.             <textarea name="insurance_comment"  placeholder=""></textarea>  
  39.         </div>  
  40.           
  41.             </div>  
  42.               
  43.               
  44.         </div>  

 

 

要想監聽“險種”文本框值變化,就需要用到input和propertychange這兩個事件,這兩個事件是監聽內容變化的。

 

Js代碼   收藏代碼
  1. dom.bind('input propertychange', function() {  
  2. //do something    
  3. });  

 

 

只要綁定好這兩個內容變化事件,然后在事件體里面編寫collapsible標題賦值代碼即可搞定問題。

 

collapsible標題對應前面HTML代碼中的"<h3></h3>"元素,但是通過$("h3").html(dom.val())這種形式給它賦值是錯誤的,這樣會導致collapsible樣式亂套。因為jquerymobile在上面html基礎上動態創建了別的節點元素和樣式,最終才會有這樣折疊的效果。

 

可以通過瀏覽器查看最終的頁面元素,你就會發現它與原本的HTML有很大的變化。


 上圖<span class="ui-btn-text"></span>才是現實標題內容的節點。

 

所以要想給collapsible標題賦值,就需要找到class為ui-btn-text的節點。

Js代碼   收藏代碼
  1. dom.parents("#collapsible").find(".h3").find(".ui-btn-text").html(dom.val());  

 

完整的javascript代碼應該這樣來寫:

Js代碼   收藏代碼
  1. $(document).on("pageinit", "#insurer_list_page", function() {  
  2.    var dom = $("#collapsible-set").find("input[name='insurance_name']");  
  3.     dom.bind('input propertychange', function() {  
  4.         var value = dom.val();  
  5.         if($.isNull(value)){  
  6.             //輸入內容為空,則以空格填充  
  7.             value = "&nbsp;";  
  8.         }  
  9.         dom.parents("#collapsible").find(".h3").find(".ui-btn-text").html(value);  
  10.     });  
  11. });  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM