如何使用javascript检查没有Id / Class的复选框? [英] How to check the checkbox without Id/Class using javascript?

查看:101
本文介绍了如何使用javascript检查没有Id / Class的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript检查没有ID / Class的复选框?

How to check the checkbox without Id/Class using javascript?

<form>
    <input type="checkbox" name="vehicle" value="Bike">bike
    <input type="checkbox" name="vehicle" value="Car">car
</form>


推荐答案

在javascript中

您可以使用 document.querySelectorAll(input [type ='checkbox'])[0] 第一个和 document.querySelectorAll(input [type ='checkbox'])[1] 为第二个。

You can use document.querySelectorAll("input[type='checkbox']")[0] for the first one and document.querySelectorAll("input[type='checkbox']")[1] for the second one.

要检查它们,你需要使用 document.querySelectorAll(input [type ='checkbox'])

To check them all you need to use document.querySelectorAll("input[type='checkbox']")

换句话说:

First_checkbox = document.querySelectorAll("input[type='checkbox']")[0];
Second_checkbox = document.querySelectorAll("input[type='checkbox']")[1];

您可以使用 document.querySelectorAll(input [type = 'checkbox']) tag:

document.querySelectorAll("input[type='checkbox']")[1].checked = true;
document.querySelectorAll("input[type='checkbox']")[1].checked = false;

在jQuery中

您可以使用 $(:checkbox)[0] 作为第一个, $(:复选框)[1 ] 第二个。

You can use $(":checkbox")[0] for the first one and $(":checkbox")[1] for the second one.

要检查它们,你需要使用 $(:checkbox)

To check them all you need to use $(":checkbox")

换句话说:

First_checkbox = $(":checkbox")[0];
Second_checkbox = $(":checkbox")[1];

使用 $(:复选框)可以做的事情 tag:

$(":checkbox")[1].checked = true;
$(":checkbox")[1].checked = false;

这篇关于如何使用javascript检查没有Id / Class的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆