Jquery mobile click () in list view

I have a list problem in jQuery mobile. I want to load some data from the server using JSON and populate the list with elements. It works great. But when I try to respond to a click on a new loaded item, I do net get event! I think I need to update the view somehow, but I don’t know how to do it.

I made a small sketch at http://jsfiddle.net/VqULm/227/

when u presses the click me button, the click event on the element is no longer tracked. How to get a "Wokrs" warning for new items?

Thanks so much for reading!

+5
source share
3 answers

Try

    $('#listview').on('click', 'li', function() {
        alert("Works"); // id of clicked li by directly accessing DOMElement property
    });

with jQuery > 1.7

Demo

OR

$('#listview li').live('click', function() {
    alert("Works"); // id of clicked li by directly accessing DOMElement property
});

with your jQuery version 1.6.4.

Demo

. li listview , li live ( jQuery) delegate (jQuery > 1.7).

+19

JQM (, my-jqm 1.2.0)

:

     <ul data-role="listview" id="myList">
       <li id="1" >text</li>
     </ul>

     //on the js code use delegate
     $('#myList').delegate('li', 'click', function () {
         alert($(this).attr('id'));
     });
+3

For remote data source and liswiew

wrap the ul element with div

<div data-role="page" id="myPage">
  <form id="myform" class="ui-filterable">
    <input id="what" data-type="search" placeholder="i.e.Carpentry...">
    <div id="w_what">
      <ul id="ul_what" data-role="listview" data-inset="true" data-filter="true" data input="#what">
      </ul>
     </div>
   </form>
 </div>

$( "#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( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
        $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 += "<li>" + val.text + "</li>";  // 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 

 });

Hope helps somenone

+2
source

All Articles