这是我的HTML:
...
<input type="checkbox" id="lm27" value="27" checked="checked"
class="charr">ismea</input>
...
当切换复选框时,应调用事件。该元素由类选择。但事件没有被召唤。
$(function(){
$('.charr').change(function() {
alert('Handler for .change() called.');
});
refresh_langlist();
});
完整的HTML,如果你想尝试:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js">
</script>
<script>
$('.charr').change(function() {
alert('Handler for .change() called.');
});
</script>
<input type="checkbox" id="lm27" value="27" checked="checked"
class="charr">ismea
</body>
</html>
答案 0 :(得分:1)
现在我意识到了这个问题:
在新动态生成的输入后始终调用selector + event。旧事件即使匹配也不适用于新元素。
答案 1 :(得分:-1)
您可以尝试更改正在使用的方法,以检查复选框是否已选中。这是一个(工作)代码示例,用于检查是否选中了复选框:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script type="text/javascript">
$(document).ready(function() {
var checked = 0;
$('input[type="checkbox"]').click(function() {
var $b = $('input[type=checkbox]');
checked = $b.filter(':checked').length;
//alert('There are [' + checked + '] checked checkboxes');
if (checked > 2) {
$("input:checkbox:not(:checked)").attr('disabled','disabled');
}else{
$('input[type=checkbox]').removeAttr('disabled');
}
});
}); //END $(document).ready()
</script>
</head>
<body>
<h2>Allow up to three checkboxes to be checked, then disable all checkboxes on page</h2>
<form action="">
<input type="checkbox" name="vehicle" value="bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="car">I have a car<br>
<input type="checkbox" name="vehicle" value="spoon">I have a spoon<br>
<input type="checkbox" name="vehicle" value="guitar">I have a guitar<br>
<input type="checkbox" name="vehicle" value="boat">I have a boat<br>
<input type="checkbox" name="vehicle" value="house">I have a house<br>
</form>
</body>
</html>