为什么在同一类中触发点击事件时不触发更改事件
why change event is not triggered while click event is triggered in same class?
嗨,我有使用 jquery 移动版创建移动应用程序的代码。BUt 在我的代码中,我动态创建了页面并计算了员工经验。由于 id 在新创建的行上生成,因此我将类视为唯一来触发事件。但是,当点击事件在同一类"to"上触发时,不会触发该更改事件。下面是我的代码。请建议如何实现这一目标?
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
<link href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.css" rel="stylesheet" />
<link rel="stylesheet" href="https://rawgithub.com/arschmitz/jquery-mobile-datepicker-wrapper/v0.1.1/jquery.mobile.datepicker.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="https://rawgithub.com/jquery/jquery-ui/1.10.4/ui/jquery.ui.datepicker.js"></script>
<script id="mobile-datepicker" src="https://rawgithub.com/arschmitz/jquery-mobile-datepicker-wrapper/v0.1.1/jquery.mobile.datepicker.js"></script>
<!-- Page Values and Script -->
<script>
$(document).ready(function() {
$("input.add").live('click', function() {
var length = 0;
var table = $(this).closest('table').attr('id');
if (table == 'Employee')
var length = $('#Employee').find('tr').length;
console.log(length);
var $tr = $(this).closest('tr');
var $clone = $tr.clone();
$clone.find('input:text').val('');
var id2 = 'To' + length;
console.log(id2);
$clone.find(".to").parent().html(' <input class="to" id="' + id2 + '" name="To" type="text" value="" data-role="date" style="width:100%" />');
$clone.find('input[data-role="date"]').date();
console.log($clone);
$tr.after($clone);
});
尝试更改此代码
$('input.to').on('change', function(){...});
到
$(document).on('change','input.to', function(){...});
我假设您使用的是jquery.1.7
版本。因此,请尝试使用如下所示的.live
:
$('input.to').live('change', function() {
或者,如果是动态添加的元素,最好使用$(document)
来侦听动态添加元素的事件
$(document).on('change','input.to', function() {
相关文章:
- 分派点击事件并保留击键修饰符
- 模糊事件的Javascript测试
- keyup事件处理程序更改焦点不适用于快速键入
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 如何使Javascript动态html表及其上的事件
- 使用类从一个标记中双击事件
- 如何在未直接触发的情况下停止事件
- 如何在elfinder插件(一个文件管理器插件)上获得上传前事件
- 对iPad上的点击事件反应缓慢
- 事件和状态
- Fancybox是否将Click事件静音
- 主干-不管怎样,检查事件以前是否绑定过
- 从控制器返回后Ajax启动事件激发
- React-具有参数的同一类中的事件
- 在同一类 JQUERY 的不同时间触发事件
- 当我将事件添加到同一类差异 ID 时会出现一些错误
- 如何使用javascript onclick事件处理程序同一类锚元素
- jquery函数用于同一类的多个按钮事件
- 同一类的多个实例上的事件处理 - 需要澄清
- 为什么在同一类中触发点击事件时不触发更改事件