如何选择下拉列表的值

时间:2018-04-28 11:48:56

标签: select phantomjs casperjs

我正在尝试使用casperJS在下拉列表中选择和选项。我的HTML

<select data-role="none" id="selectStatus">

<option value="SINGLE">Single</option>
<option value="MARRIED_FILING_JOINTLY">Married Filing Jointly</option>
<option value="MARRIED_FILING_SINGLY">Married Filing Separately</option>
<option value="HEAD_OF_HOUSEHOLD">Head of Household</option></select>

我的JS是

var status = 2; // Single , Married Filing Jointly , Married Filing Separately , Head of Household
//trying different ideas here... 
if(status == 1){
            status = 'SINGLE';
        }else if(status == 2){
            status = 'HEAD_OF_HOUSEHOLD';   
        }else if(taxFilingStatus == 3){
            status = 'MARRIED_FILING_JOINTLY';  
        }else if(taxFilingStatus == 4){
            status = 'MARRIED_FILING_SINGLY';   
        }

this.evaluate(function () {
        $('#selectStatus').val(status).change();
    });
    this.echo(status);
    this.capture(CapturePath('StatusSelected.png'));

作为变量传递时,我没有选择状态,但是当我像这样传递变量时

$('#selectStatus').val('SINGLE').change();

任何暗示这里有什么问题?传递字符串有任何限制吗?

1 个答案:

答案 0 :(得分:0)

您没有在status内传递page.evaluate变量,因此它在浏览器上下文中未定义,因此无效。

传递它的正确方法:

this.evaluate(function (status) {
    $('#selectStatus').val(status).change();
}, status);