列表视图上的jquery mobile click()
我在jquery mobile中的listview有问题。 我想使用JSON从服务器加载一些数据,并用项目填充listview。 这很好。 但是当我试图通过点击新加载的项目做出反应时,我会做网络活动! 我想我必须以某种方式刷新视图,有点不知道如何。
我在http://jsfiddle.net/VqULm/227/做了一个小草图
当你点击我点击按钮时,不再跟踪项目上的点击事件。 如何在新物品上获得“Wokrs”警报?
非常感谢你的阅读!
尝试
$('#listview').on('click', 'li', function() { alert("Works"); // id of clicked li by directly accessing DOMElement property });
用jQuery > 1.7
DEMO
要么
$('#listview li').live('click', function() { alert("Works"); // id of clicked li by directly accessing DOMElement property });
使用您的jQuery版本1.6.4。
DEMO
为什么你需要这个
因为。 你在页面重新加载后在listview
添加li
,所以那些li
的任何事件都应该live
(对于你使用的jQuery版本)或delegate
(jQuery> 1.7)。
如果您的JQM版本是旧的(如mine-jqm 1.2.0),并且两种提到的方法都不适合您
试试这个:
- text
//on the js code use delegate $('#myList').delegate('li', 'click', function () { alert($(this).attr('id')); });
到远程数据源和liswiew
用div包装ul元素
$( "#ul_what" ).on( "filterablebeforefilter", function ( e, data ) { var $ul = $( this ), $input = $( data.input ), value = $input.val(), html = ""; $ul.html( "" ); // initially null $('#w_what').show(); // For next search if ( value && value.length > 0 ) { $ul.html( " " ); $ul.listview( "refresh" ); $.ajax({ url: "/php/getWhat.php", // Ajax url dataType: "json", // json data: { q: $input.val() // which value to be searched } }) .then( function ( response ) { $.each( response, function ( id, val ) { html += "" + val.text + " "; // json response has text element }); $ul.html( html ); $ul.listview( "refresh" ); $ul.trigger( "updatelayout"); }); } }); $('#ul_what').delegate('li', 'click', function () { var ul = $(this); // when clicked $('#what').val(ul.text()); // set the value for input $('#w_what').hide(); // hide the div });
希望对某些人有所帮助