2013-03-27 19 views
1

我使用jQuery來創建一個列表視圖和過濾器。如果用戶點擊某個項目,我希望過濾器將文本設置爲點擊項目並隱藏列表。但不是永久!如果用戶更改過濾器文本,則需要返回列表。jQuery的ListView的 - 如何隱藏項目被點擊時的列表

以下是我有:

$(document).ready(function() { 

     jQuery.support.cors = true; 
     $("#groupSelectList").listview({ 
      filter: true, 
      filterPlaceholder: '', 
      icon: false 
     }); 

     $('#groupSelectList').children('li').on('click', function() { 
      $('#groupName').val($(this).text()); 
      $("input[data-type='search']").val($(this).text()) 
      //$("#groupSelectList").listview("refresh");//this refresh doesn't appear to do anything. 
      $("#groupSelectList").listview().hide()//hide works, but the list won't come back if the user changes the input text. 
     }); 
    }); 

...

此頁面上的小部件是我使用的控制的一個例子: http://view.jquerymobile.com/master/demos/

+0

我有相同的頁面上有兩個jQuery Mobile的列表視圖和這個$( 「輸入[數據類型= '搜索']」)VAL($。 (this).text()),將值分配給文本搜索。有沒有什麼辦法可以將id定義爲「input [data-type ='search'」 – 2015-05-07 13:20:15

回答

1

我想通了如何做到這一點。如果有人感興趣,爲了讓jQuery移動列表視圖控件在點擊時消失,然後在更改時重新出現,我將一個臨時的鍵盤事件綁定到搜索框。我的代碼現在看起來像這樣:

var groupSelectHandler = function() { 
     $("#groupSelectList").listview().show() 
     $("#groupSelectList").listview("refresh"); 
     $("input[data-type='search']").unbind('keyup', groupSelectHandler); 
    } 

    $(document).ready(function() { 
     $("#groupSelectList").listview({ 
      filter: true, 
      filterPlaceholder: '' 

     }); 

     $('#groupSelectList').children('li').on('click', function() { 
      $('#groupName').val($(this).text()); 
      $("input[data-type='search']").val($(this).text()); 
      $("#groupSelectList").listview().hide(); 
      $("input[data-type='search']").bind('keyup', groupSelectHandler); 
     }); 
    }); 

您可以在這個例子中,當點擊觸發groupSelectHandler勢必KEYUP看到。當keyup激發時,它顯示列表並刪除keyup事件。

1

可能的,更簡單的解決方案可能是:

$(document).on("pageinit", "#myPage", function() { 
    $('#groupName').val($(this).text()); 
     $("input[data-type='search']").val($(this).text());; 
    $('#groupSelectList li').each(function (index) { 
     $(this).addClass("ui-screen-hidden"); 
    }); 
}); 
相關問題