javascript/jquery获取新选中复选框的值

javascript/jquery获取新选中复选框的值,javascript,php,jquery,Javascript,Php,Jquery,下面是我的代码 <input type="checkbox" id="checkbox" value="brand.php?brand=bmw" name="bmw" <?php echo $checked; ?> > BMW</li> <input type="checkbox" id="checkbox" value="brand.php?brand=toyota" name="toyota"> Toyota</li> <in

下面是我的代码

<input type="checkbox" id="checkbox" value="brand.php?brand=bmw" name="bmw" <?php echo $checked; ?> > BMW</li>
<input type="checkbox" id="checkbox" value="brand.php?brand=toyota" name="toyota"> Toyota</li>
<input type="checkbox" id="checkbox" value="brand.php?brand=farari" name="farari" checked > Farari</li>
<input type="checkbox" id="checkbox" value="brand.php?brand=nissan" name="nissan" checked > Nissan</li>

$(文档).ready(函数(){
var ckbox=$('.clscheckbox');
var url='';
$('input[type=checkbox]')。更改(函数(){
如果($(this).is(':checked')){
var url=$(this.val();
console.log(url);
警报(url);
} 
});
});


id
应该是整个文档中唯一的值。更改HTML以使每个复选框具有唯一的id,并根据类或输入类型修改jQuery选择器

检查下面的示例

$(文档).ready(函数(){
$('.chkbox')。在('change',function()上{
如果($(this).is(':checked')){
var url=$(this.val();
console.log(url);
window.location=url;
}
});
});

宝马
丰田
法拉利

日产
又一个示例代码。希望能奏效

JQuery代码

$( ".checkbox" ).click(function() {
    if ($(this).is(":checked"))
    {
        var page_url = $(this).val();
        window.location = page_url;
    }
});
HTML

<input type="checkbox" class="checkbox" value="brand.php?brand=bmw" name="bmw"> BMW</li>
<input type="checkbox" class="checkbox" value="brand.php?brand=toyota" name="toyota"> Toyota</li>
<input type="checkbox" class="checkbox" value="brand.php?brand=farari" name="farari"> Farari</li>
<input type="checkbox" class="checkbox" value="brand.php?brand=nissan" name="nissan"> Nissan</li>
BMW
丰田
法拉利
日产

多个
id
具有相同的
名称
我从未尝试过此功能,id不应重复
[id=“checkbox”]
全局属性定义了一个唯一标识符(id),该标识符在整个文档中必须是唯一的。“转到新选中的复选框”是什么意思?最好在jquery中使用类来获取复选框,并为它们提供唯一的ID。
if(ckbox.is(':checked'){
即使选中其中一个复选框,也会返回true。这不应该是正确的方法。如果取消选中
Farari
,它仍然会导航到Farari页面。
<input type="checkbox" class="checkbox" value="brand.php?brand=bmw" name="bmw"> BMW</li>
<input type="checkbox" class="checkbox" value="brand.php?brand=toyota" name="toyota"> Toyota</li>
<input type="checkbox" class="checkbox" value="brand.php?brand=farari" name="farari"> Farari</li>
<input type="checkbox" class="checkbox" value="brand.php?brand=nissan" name="nissan"> Nissan</li>