我无法禁用使用jQuery从另一个PHP填充的某些复选框。我想我的错误来自哪里$(document).ready
。请帮忙,谢谢!
PHP:
$result = mysqli_query($con, 'SELECT * FROM table WHERE user="'.$_POST['user'].'%"');
while ($row = mysqli_fetch_assoc($result)) {
echo '<input type="checkbox" value="'.$row['user'].'" data-tag="'.$row['tag'].'" />'.$row['user'];
}
jQuery:
$(document).ready(function() {
$('#showResult').click(function() {
$.ajax({
type: 'post',
url: 'load_result.php',
data: { user: $('#userSelect').val() }
success: function(data) {
$('#result').html(data);
}
});
$('input[type="checkbox"]').on('click', function() {
var $this = $(this);
var tag = $this.data('tag');
if ($this.is(':checked')) {
$('input[type="checkbox"]:not([data-tag*="'+tag+'"])').prop('disabled', true);
} else {
$('input[type="checkbox"]:not([data-tag*="'+tag+'"])').prop('disabled', false);
}
});
});
由于正在创建新的元素,你做事件绑定之后,你必须使用的装置,该装置绑定点击所有的复选框和新的事件。
您的代码应如下所示:
$('body').on('click', 'input[type="checkbox"]',function() {
var $this = $(this);
var tag = $this.data('tag');
if ($this.is(':checked')) {
$('input[type="checkbox"]:not([data-tag*="'+tag+'"])').prop('disabled', true);
} else {
$('input[type="checkbox"]:not([data-tag*="'+tag+'"])').prop('disabled', false);
}
});
在过去的版本中,您可以使用,live();
但从1.7开始不推荐使用,必须使用on()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句