Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用文本从下拉列表中选择值_Javascript_Html Select_Casperjs - Fatal编程技术网

Javascript 使用文本从下拉列表中选择值

Javascript 使用文本从下拉列表中选择值,javascript,html-select,casperjs,Javascript,Html Select,Casperjs,如何使用文本而不是值或索引从下拉列表中选择值? HTML: <select name="category_group" id="category_group" sel_id="" > <option value="0" selected="selected">Kies de rubriek</option> <option value='1000' style='background-color:#dcdcc3;font-weig

如何使用文本而不是值或索引从下拉列表中选择值? HTML:

<select   name="category_group" id="category_group"  sel_id="" >
    <option value="0" selected="selected">Kies de rubriek</option>

    <option value='1000' style='background-color:#dcdcc3;font-weight:bold;' disabled="disabled" id='cat1000' >

            -- VOERTUIGEN --

    </option> 

    <option value='1020'  id='cat1020' >
        Auto's

    </option> 

    <option value='1080'  id='cat1080' >
        Auto's: Onderdelen

    </option> 

    <option value='1040'  id='cat1040' >
        Motoren

    </option> 

    <option value='1140'  id='cat1140' >
        Motoren: Onderdelen

    </option>
</select>   
这不起作用,但它使用“Motoren”的值(即1140)起作用。
如何使用FillSelector使其与文本一起工作?

CasperJS'
fill
函数仅通过使用值工作。在您的情况下,这不起作用,因为您试图设置显示的值而不是指定的选项值。不过,这可以很容易地扩展:

casper.selectOptionByText = function(selector, textToMatch){
    this.evaluate(function(selector, textToMatch){
        var select = document.querySelector(selector),
            found = false;
        Array.prototype.forEach.call(select.children, function(opt, i){
            if (!found && opt.innerHTML.indexOf(textToMatch) !== -1) {
                select.selectedIndex = i;
                found = true;
            }
        });
    }, selector, textToMatch);
};

casper.start(url, function() {
    this.selectOptionByText('form[name="formular"] select[name="category_group"]', "Motoren");
}).run();

有关SO联系人页面上的完整工作示例,请参见。

CasperJS'
fill
函数仅通过使用值工作。在您的情况下,这不起作用,因为您试图设置显示的值而不是指定的选项值。不过,这可以很容易地扩展:

casper.selectOptionByText = function(selector, textToMatch){
    this.evaluate(function(selector, textToMatch){
        var select = document.querySelector(selector),
            found = false;
        Array.prototype.forEach.call(select.children, function(opt, i){
            if (!found && opt.innerHTML.indexOf(textToMatch) !== -1) {
                select.selectedIndex = i;
                found = true;
            }
        });
    }, selector, textToMatch);
};

casper.start(url, function() {
    this.selectOptionByText('form[name="formular"] select[name="category_group"]', "Motoren");
}).run();

请参阅SO联系人页面上的完整工作示例。

很抱歉不清楚,但“Motoren”的值(即“1040”)起作用。因此,这个.fillSelectors('form[name=“formular”],{'select[name=“category_group”]:'1040',false);works您在此页面上有jquery吗?很抱歉不清楚,但是“Motoren”的值(即“1040”)有效。因此,这个.fillSelectors('form[name=“formular”],{'select[name=“category_group”]:'1040',false);工作页面上有jquery吗?