选择选项元素JavaScript控制台

时间:2019-05-08 17:21:34

标签: javascript html click html-lists

我想从列表JavaScript中选择100,我正在控制台中尝试

<div class="dataTables_length" id="Table_6_length">
 <label>
     <select name="DataTables_Table" aria-controls="DataTables_Table_6" class="form-control input-sm">
              <option value="10">10</option>
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
        </select>
</label></div>

我是JavaScript新手,请帮助我

3 个答案:

答案 0 :(得分:1)

您可以像这样循环选择选项

let sel = document.getElementsByName('DataTables_Table')[0];

function getSelectedOption(sel) {
    var opt;
    for ( var i = 0, len = sel.options.length; i < len; i++ ) {
        opt = sel.options[i];
        if ( opt.value == 100 ) {
            break;
        }
    }
    return opt;
}

let opt100 = getSelectedOption(sel);
console.log(opt100)
console.log(opt100.value)

let sel = document.getElementsByName('DataTables_Table')[0];

function getSelectedOption(sel) {
    var opt;
    for ( var i = 0, len = sel.options.length; i < len; i++ ) {
        opt = sel.options[i];
        if ( opt.value == 100 ) {
            break;
        }
    }
    return opt;
}

let opt100 = getSelectedOption(sel);
console.log(opt100)
console.log(opt100.value)
<div class="dataTables_length" id="Table_6_length">
 <label>
     <select name="DataTables_Table" aria-controls="DataTables_Table_6" class="form-control input-sm">
              <option value="10">10</option>
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
        </select>
</label></div>

答案 1 :(得分:0)

select提供一个ID 然后就这么简单 document.getElementById('selecter').value=100;

refer this.

答案 2 :(得分:0)

您只需将.value的{​​{1}}属性使用

dropdown element
function selectOption(val) {

  document.getElementById('dataTable').value = val;

}