简体   繁体   English

用Javascript一线来确保已选中复选框?

[英]Javascript one liner to make sure checkbox is checked?

I am working with selenium ide and was wondering if there is a one line that can make sure a checkbox is checked. 我正在使用硒ide,想知道是否有一行可以确保选中复选框。

or any other selenium javascript commands you've used in the past would be greatly appreciated. 或您过去使用过的任何其他硒javascript命令,将不胜感激。

Thanks all!! 谢谢大家!

If you want to return the value (read whether true or false): just add ".checked" to the end of the object that represents the checkbox, let's pretend our checkbox has an id="checkBox": 如果要返回值(读取是对还是错):只需在代表复选框的对象的末尾添加“ .checked”,我们就可以使复选框具有id =“ checkBox”:

checkBox.checked  // returns true or false.

If you want to set the checked attribute to checked 如果要将检查属性设置为检查

checkBox.checked = true;

Looking at the link in the comment, it looks like you're using query selector. 查看评论中的链接,看起来您正在使用查询选择器。 Unlike jQuery there's no built in each on the array like list returned by the querySelectorAll method... 与jQuery不同的是,在数组中没有内置每个像querySelectorAll方法返回的列表一样的列表...

In one line, it is messy... 在一行中,这很杂乱...

To get: 要得到:

Array.prototype.forEach.call(document.querySelectorAll('input[type="checkbox"]'), function(checkbox){return checkbox.checked; });

To set 设置

Array.prototype.forEach.call(document.querySelectorAll('input[type="checkbox"]'), function(checkbox){checkbox.checked = true; });

edit: Added return keyword to the "get" version. 编辑:在“获取”版本中添加了return关键字。

edit2: a jsfiddle: http://jsfiddle.net/snlacks/pLgjx4xa/ edit2:一个jsfiddle: http//jsfiddle.net/snlacks/pLgjx4xa/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM