html - 动态列表项上的 jQuery Mobile 单击事件

标签 html jquery-mobile

我无法从列表项中获取点击事件。在这个页面中:

http://bec-systems.com/list-click.html

列表中的第一个条目触发点击事件。但是,如果我通过按下“刷新更新列表”按钮动态添加 3 个事件,则接下来的 3 个列表条目不会生成点击事件。

感谢关于我如何使这项工作或总体上改进代码的任何建议。

谢谢, 悬崖

代码也列在下面:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Status</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
  <script type="text/javascript">
$(document).ready(function() {
  $("#refreshUpdateButton").on("click", function(event, ui) {
    console.log("refreshUpdateButton")

    versions = ["0.3", "0.4", "0.5"]

    for (var i=0; i < versions.length; i += 1) {
      $("#updateVersionsList").append('<li><a id="updateVersionItem-' + (i+3) + '">' + versions[i] + '</a></li>');
      if ($("#updateVersionsList").hasClass('ui-listview')) {
        $("#updateVersionsList").listview("refresh");
      } else {
        $("#updateVersionsList").trigger('create');
      }
    }

  })

  $('[id^=updateVersionItem]').on("click", function(event, ui) {
    console.log("updateVersion, selected = " + $(this).attr('id'));
  })

});

  </script>
</head> 
<body> 

<!-- Software update page -->
<div data-role="page" id="software-update-page">
    <div data-role="header">
        <h1>Software Update</h1>
    </div><!-- /header -->
    <div data-role="content">   
    <h1>Select Software version:</h1>
    <ul data-role="listview" id="updateVersionsList">
      <li><a id="updateVersionItem-0">0.0</a></li>
      <li><a id="updateVersionItem-1">0.1</a></li>
      <li><a id="updateVersionItem-2">0.2</a></li>
    </ul>
    <br>
    <a data-role="button" class="ui-btn-left" id="refreshUpdateButton">Refresh Update list</a>
    </div><!-- /content -->
</div>

</body>
</html>

最佳答案

使用这种形式的 .on()(根据下面的评论)。

  $(document).on("click", '[id^=updateVersionItem]', function(event, ui) {
    console.log("updateVersion, selected = " + $(this).attr('id'));
  })

示例:http://jsfiddle.net/saluce/YaAEJ/

否则,无论何时动态添加新元素,都需要将点击事件附加到这些项目。

假设以下代码:

function doThisOnClick(event, ui) {
    console.log("updateVersion, selected = " + $(this).attr('id'));
}

$('[id^=updateVersionItem]').on("click", doThisOnClick);

您可以取消绑定(bind)处理程序并重新附加到所有匹配项:

$('[id^=updateVersionItem]').off("click", doThisOnClick);
$('[id^=updateVersionItem]').on("click", doThisOnClick);

或者在添加后将其动态添加到新项目中:

$("#updateVersionsList").append('<li><a id="updateVersionItem-' + (i+3) + '">' + versions[i] + '</a></li>').on("click", doThisOnClick);

关于html - 动态列表项上的 jQuery Mobile 单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11458456/

相关文章:

javascript - 当所有按钮具有相同值时如何检查单击了哪个按钮

jquery - 在 jQuery Mobile 中设置按钮颜色?

html - 如何从 perl 网站中提取特殊类型的表格?

html - 具有延迟和外观的水平滚动文本问题

javascript - 选择 <option> 时动画 CSS

html - xsl for-each : add code block every n rows?

javascript - Jquery Mobile 中的选择选项问题

页面刷新后 JQuery 验证不起作用 (Jquery Mobile)

javascript - 选项卡中的 jQuery Mobile ListView 和渲染问题

javascript - 如何使用 jquery mobile 编辑 RSS 提要