简体   繁体   English

如何使用CasperJS检索复选框的值

[英]How to retrieve value of a checkbox with CasperJS

I have an issue fetching the value true/false for a checkbox in my webpage with casperjs! 我在使用casperjs获取网页中复选框的值true / false时遇到问题! I did read several other threads for this subject but it did not solved my issue. 我确实阅读了该主题的其他几个主题,但是并没有解决我的问题。

See my webpage code: 查看我的网页代码: 在此处输入图片说明

This is my CasperJS code that I have until now and that finds the id that I need for the checkbox: 这是我到目前为止拥有的CasperJS代码,它找到复选框所需的ID:

...
    var numTimes = 20,
    count = -1;
    casper.repeat(numTimes, function() {
        var name = this.evaluate(function(count) {
            var sel = '[id*="connectedToNeType[' + count + ']"]';
            var element = document.querySelector(sel).id;
            return element;
        }, ++count);
        var check_id = name.replace(/_TD|_TR/, '');
        var checked = this.evaluate(function(check_id) {
            var element = document.getElementById(check_id).checked;
            return element;
        });
        this.echo(' checkbox: ' + checked);
    });

I am using repeat because I have multiple id's that I need to check the value of the check boxes! 我使用重复是因为我有多个ID,需要检查复选框的值!

The variable name would fetch the several id's from similar lines like the below line: 变量名将从类似下面的行中获取多个id:

在此处输入图片说明

In an example page, I get the below printout when running the above CasperJS: 在示例页面中,运行上面的CasperJS时,我得到下面的打印输出:

-> connectedToNeType[0]}
 checkbox: null
-> connectedToNeType[1]}
 checkbox: null
-> connectedToNeType[2]}
 checkbox: null
-> connectedToNeType[3]}
 checkbox: null
-> connectedToNeType[4]}
 checkbox: null
-> connectedToNeType[5]}
 checkbox: null

You forgot to pass in the check_id into the second evaluate() call. 您忘记了将check_id传递给第二个check_id evaluate()调用。 Also, your checkbox ends in "_CB" which you haven't added. 另外,您的复选框以“ _CB”结尾,您尚未添加。

It seems you want: 看来您想要:

var name = this.evaluate(function(count) {
    var sel = '[id*="connectedToNeType[' + count + ']"]';
    var element = document.querySelector(sel).id;
    return element;
}, ++count);
var check_id = name.replace(/_TD|_TR/, '') + '_CB';
var checked = this.evaluate(function(check_id) {
    var element = document.getElementById(check_id).checked;
    return element;
}, check_id);
this.echo(' checkbox: ' + checked);

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

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