【问题标题】:casperjs: can't get jquery to use global variablescasperjs:无法让 jquery 使用全局变量
【发布时间】:2016-07-05 21:37:12
【问题描述】:

我希望这是一个简单回答的愚蠢问题。
(我在谷歌上搜索了一天半,没有任何乐趣)

我正在编写一个更改下拉菜单的 casperjs 脚本
我已经简化了测试代码以解决问题的症结
我的测试HTML如下:

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    </head>
    <body style="background-color:powderblue;">
    <form>
    <select id="down">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="vw">VW</option>
      <option value="audi">Audi</option>
    </select>
    </form>
    </body>
    </html>

使用 jquery 的工作 Casperjs 脚本:

casper.start("http://192.168.0.14/test.html", function(){
   //change the pulldown selection
   casper.then(function () {
        this.evaluate(function(){
            $('#down').val('vw').change();
        });
   });

   casper.then(function(){
           this.capture("screen.png");
   });
});
casper.run();

现在我想参数化代码,并为 selectorvalue 使用变量而不是字符串。但是这段代码不起作用:

var x1='#down';
var y1='vw';

casper.start("http://192.168.0.14/test.html", function(){
   //change the pulldown selection
   casper.then(function () {
        this.evaluate(function(){
            $(x1).val(y1).change();
        });
   });

   casper.then(function(){
           this.capture("screen.png");
   });
});
casper.run();

这应该不难(而且可能不难),但“窗口”的所有组合。或方括号符号让我失望了。
jquery 拒绝玩好。

请帮忙,我不认为这会让我超出我的深度,但它显然有

【问题讨论】:

    标签: javascript jquery global-variables casperjs


    【解决方案1】:

    试试这个:-

        this.evaluate(function(x1, y1){
            $(x1).val(y1).change();
        }, x1, y1);
    

    evaluate 里面的任何东西都是沙盒的,你需要传入你想在里面使用的任何参数

    【讨论】:

    • 就是这样!你先生知道你在说什么。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多