无法在php/javascript中选中foreach循环中的所有其他复选框
我目前在cakephp2项目中遇到javascript问题。我想做的是,在选中/取消选中Select all country(选择所有国家/地区)时,选中和取消选中foreach循环中的所有其他复选框。我在网上搜索过,但我发现很多教程都很难。一些技巧或例子将是伟大的!我很想听到你的消息。 我有下面的代码无法在php/javascript中选中foreach循环中的所有其他复选框,javascript,php,jquery,Javascript,Php,Jquery,我目前在cakephp2项目中遇到javascript问题。我想做的是,在选中/取消选中Select all country(选择所有国家/地区)时,选中和取消选中foreach循环中的所有其他复选框。我在网上搜索过,但我发现很多教程都很难。一些技巧或例子将是伟大的!我很想听到你的消息。 我有下面的代码 <ul> <li> <label> <input type="checkbox"> <span clas
<ul>
<li>
<label>
<input type="checkbox">
<span class="checkBoxImage">
<?= $Worldarea['name'] ?>Select all country
</span>
</label>
</li>
<? foreach ($Worldarea['world']['data'] as $world) {
if (empty ($world['count'])) continue;
$select = array_key_exists($world['id'], $info['query']['area_info']);
?>
<li>
<label for="area<?= $world['id'] ?>" alt="<?= $world['area_name'] ?>"
<?= ($select) ? 'class="selected"' : '' ?>>
<input type="checkbox" name="area" value="<?= $world['id'] ?>" id="area<?= $world['id'] ?>"
<?= ($sel) ? 'checked="checked"' : '' ?>>
<span class="checkBoxImage">
<?= $world['name_display'] ?>
</span>
</label>
</li>
<? } ?>
</ul>
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
首先,您需要将id属性添加到selectall复选框中。在本例中,我使用了复选框ALL。 之后,您应该更改您的复选框名称。使用name[]而不是name来创建复选框数组。 之后,您可以执行以下操作:
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
jQuery('#checkbox-ALL').on('click', function () {
jQuery("input[name*=area]").prop('checked', this.checked);
});
首先,您需要将id属性添加到selectall复选框中。在本例中,我使用了复选框ALL。 之后,您应该更改您的复选框名称。使用name[]而不是name来创建复选框数组。 之后,您可以执行以下操作:
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
jQuery('#checkbox-ALL').on('click', function () {
jQuery("input[name*=area]").prop('checked', this.checked);
});
我已经更改了您提供的HTML部分,通过删除PHP并使其看起来像生成的HTML,使其易于理解。因为你的PHP没有问题
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
首先,我在您的复选框中添加了一个ID,它应该选择all。这样我就可以在每次检查或取消检查时附加一个事件onChange进行检测。在这种情况下,我会在同一个列表中找到所有复选框,并根据“全选”框的当前值将它们应用于checked属性
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
$checkall.changefunction{
var is_check=$this.prop'checked';
$this.closest'ul'。查找'input[type=checkbox]'。属性'checked',是\u check;
};
选择所有国家/地区
国名
国名
国名
国名
我已经更改了您提供的HTML部分,通过删除PHP并使其看起来像生成的HTML,使其易于理解。因为你的PHP没有问题
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
首先,我在您的复选框中添加了一个ID,它应该选择all。这样我就可以在每次检查或取消检查时附加一个事件onChange进行检测。在这种情况下,我会在同一个列表中找到所有复选框,并根据“全选”框的当前值将它们应用于checked属性
$(this).parents().eq(1).next().find('li input').each(function(){
$(this).prop('checked','');
$(this).next().removeClass('selected');
$(this).parent().css('background', 'none');
});
$checkall.changefunction{
var is_check=$this.prop'checked';
$this.closest'ul'。查找'input[type=checkbox]'。属性'checked',是\u check;
};
选择所有国家/地区
国名
国名
国名
国名
您应该发布生成的html而不是php。@jeroen是对的。下次您需要javascript帮助时,无需发布PHP。。。生成的html将更容易帮助您。您应该发布生成的html而不是php。@jeroen是对的。下次您需要javascript帮助时,无需发布PHP。。。生成的html将使我们更容易帮助您。