使用javascript按值查找复选框 [英] Find checkboxes by value with javascript

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

问题描述

我已使用此JavaScript查找所需的复选框并进行了检查。

I have used this JavaScript to find the checkboxes I need and check them.

<script type="text/javascript">
function checkAll(c) {
    var chks = document.getElementsByName(c.name);
    for (var i = 0; i < chks.length; i++) {
        chks[i].checked = c.checked;
    }
}

但是我不能再使用它了,不知道是否可以通过它们的值名称找到它们,可以说我有3个这样显示的复选框。

But I cannot use that anymore and wonder if I could find them by their value names, lets say I have 3 checkboxes that are rendered like this..

<input name="2" type="checkbox" value="chk2" onclick="checkAll(this)">
<input name="3" type="checkbox" value="chk2" onclick="checkAll(this)">
<input name="4" type="checkbox" value="chk3" onclick="checkAll(this)">

如果我随后选中了值为 chk2的复选框之一,则应同时选中这两个复选框,但没有一个值等于chk3的人。

If I then check one of the checkboxes with value="chk2" both of them should be checked, but no the one that have a value equals to chk3. Is this possible?

推荐答案

尝试 querySelectorAll

var chks = document.querySelectorAll("input[type='checkbox'][value='chk2']");

不需要jQuery,纯Vanilla JavaScript!

No need in jQuery , pure Vanilla JavaScript!

这篇关于使用javascript按值查找复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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