我試圖從選擇選項列表中獲取值,我也是通過javascript生成的,但由於某些原因我無法檢索它。未能從select中獲取值(值未定義)
var a, i, v;
v = document.querySelector('#video-setting');
a = document.querySelector('#audio-setting');
i = 0;
while (i < audioInputDevices.length) {
a.innerHTML += '<option value=' + audioInputDevices[i].deviceId + '>' + audioInputDevices[i].label + '</option>';
i++;
}
i = 0;
while (i < videoInputDevices.length) {
v.innerHTML += '<option value=' + videoInputDevices[i].deviceId + '>' + videoInputDevices[i].label + '</option>';
i++;
}
return;
var publisherOptions;
$(document).ready(function() {
var au, audioValue, vi, videoValue;
au = document.getElementById("audio-setting");
audioValue = au.options[au.selectedIndex].value;
vi = document.getElementById("video-setting");
videoValue = vi.options[vi.selectedIndex].value;
});
publisherOptions = {
audioSource: audioValue,
videoSource: videoValue
};
和我的HTML:
<select id="video-setting"></select>
<select id="audio-setting"></select>
現在我得到的錯誤是:
Uncaught ReferenceError: audioValue is not defined
您已經在document.ready回調中聲明瞭「audioValue」和「videoValue」,然後您試圖將它們分配給一個完全不同範圍內的對象字面值。 –