我一直在尝试通过javascript从“选择”元素中检索选定的值。我甚至无法使用jQuery或document.getElementById()从DOM获取元素。 这里是我的代码:无法通过ID获取HTML元素
function getDropdown(headerText, options, id) {
var container = document.createElement('div');
container.setAttribute('class', 'nfvo-drop-down-container');
var dd = document.createElement('select');
$(dd).attr('id', id);
for(var val in options) {
$('<option />', {value: val, text: options[val]}).appendTo(dd);
}
if(headerText) {
var span = document.createElement('span');
span.setAttribute('class', 'panel-header');
span.appendChild(document.createTextNode(headerText));
container.appendChild(span);
}
container.appendChild(dd);
return container;
}
var dd = getDropDown('My dropdown', [1,2,3,4], 'ddID);
var e = document.getElementById('ddID');
var e2 = $('#ddID');
var e3 = $('#ddID').val();
var e4 = $('#ddID').text();
如果我CONSOLE.LOG这些价值,我会得到这些结果:上什么可能导致此问题
console.log(e) -> 'null'
console.log(e2) -> <Got the element>
console.log(e3) -> 'undefined'
console.log(e4) -> <Nothing, just a blank>
任何的IDE? 此的码块是“然后”函数调用e.g内:
$.when(...).then(function(...)
<Here is the code>
);
编辑:有其中现在已经固定后一个错字。这不是我的问题的根源。
我还应该提到,我通过Dojo工具包将此div添加到ContentPane,并且我可以通过chrome中的检查工具在DOM中看到下拉列表。
有一个代码行上的错误var e = document.getElemetnById('ddID');: 其'getElementById' –
请使用控制台进行调试。 –
除此之外,你有一个语法错误,并提到'getElemetnById'中的拼写错误:为什么你确定'console.log(e2)'显示你的元素?即使没有找到元素,'$('#ddID')'的结果也不会是'null'或'undefined',因为$(...)返回一个包含零个或多个元素的结果集。如果'document.getElementById('ddID');'为'null',那么'$('#ddID')。length'将为'0'。 –