【问题标题】:I cannot set the default of a drop downlist to the last item我无法将下拉列表的默认设置为最后一项
【发布时间】:2013-12-10 03:29:18
【问题描述】:

我无法将下拉列表的默认值设置为最后一项。如果下拉列表中有 3 个项目,我希望第三个作为下拉列表的默认值。 填充下拉列表的html。

<div class='select'>
<select id='videoSource'></select>
</div>

javascript 打开相机并在电脑上显示可用的相机。有人可以帮我解决这个问题吗?

<script type="text/javascript">

var videoElement = document.querySelector("video");
var videoSelect = document.querySelector("select#videoSource");
var startButton = document.querySelector("button#start");

navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

function gotSources(sourceInfos) {
    for (var i = 0; i != sourceInfos.length; ++i) {
        var sourceInfo = sourceInfos[i];
        var option = document.createElement("option"); 
        option.value = sourceInfo.id;
        if (sourceInfo.kind === 'video') {
            option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
            videoSelect.appendChild(option);                   
        } else {
            console.log('Some other kind of source: ', sourceInfo);
        }
    }
}

if (typeof MediaStreamTrack === 'undefined') {
    alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
} else {
    MediaStreamTrack.getSources(gotSources);
}


function successCallback(stream) {
    window.stream = stream; // make stream available to console
    videoElement.src = window.URL.createObjectURL(stream);
    videoElement.play();

  }

function errorCallback(error) {
    console.log("navigator.getUserMedia error: ", error);
}

function start() {
    if (!!window.stream) {
        videoElement.src = null;
        window.stream.stop();
    }

    var videoSource = videoSelect.value;
    var constraints = {
        video: {
            optional: [{ sourceId: videoSource}]
        }
    };
    navigator.getUserMedia(constraints, successCallback, errorCallback);
}


videoSelect.onchange = start;

start();

【问题讨论】:

    标签: javascript


    【解决方案1】:

    对于最后一个选项,在 for 循环中将 selected 属性设置为 true。

    if( i == sourceInfos.length - 1 ) {
        option.selected = true;
    }
    

    【讨论】:

    • sourceInfos 中的最后一个元素可能没有 kind=='video' 在这种情况下它不会出现在选择列表中。
    • @isick 也许吧。但确切地说是last option,那么以上是明智的解决方案。同样使用selected 属性只有我们可以知道默认元素。希望你能理解。
    • 我了解您对使用所选属性的偏好,但您查找最后一个选项的算法不充分。尝试实施您的解决方案的其他人了解它存在明显(且可避免的)失败案例,这一点很重要。
    【解决方案2】:

    最简单的解决方案是在 gotSources 函数中附加新选项后添加

    videoSelect.value = sourceInfo.id;
    

    所以它会读

        if (sourceInfo.kind === 'video') {
            option.text = sourceInfo.label || 'camera ' + (videoSelect.length + 1);
            videoSelect.appendChild(option);                   
            videoSelect.value = sourceInfo.id; //update the value to the last element
        } else {
            console.log('Some other kind of source: ', sourceInfo);
        }
    

    这将为添加的每个新选项设置它(不是最“有效”的解决方案,但绝对不会降低性能)。如果您想准确地发出 value set 命令一次,您需要存储最后添加的选项并将其设置在 for 循环之外。

    同样,可能不值得,但您的电话。

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 1970-01-01
      • 2016-08-23
      • 2013-10-23
      • 2016-11-16
      • 1970-01-01
      • 2020-07-06
      • 2013-11-23
      • 1970-01-01
      相关资源
      最近更新 更多