Javascript 获取动态创建的下拉列表的ID时出错

Javascript 获取动态创建的下拉列表的ID时出错,javascript,jquery,Javascript,Jquery,我有一个div,并在它后面添加了一个下拉列表。一切正常,但当我尝试获取该下拉列表的选定值时,在浏览器控制台上出现错误“无法读取未定义的属性'undefined'” 代码: var selected_index_book = document.getElementById("surah_selection"); var book_sel = selected_index_book.options[selected_index_book.selectedIndex].value; var book_

我有一个div,并在它后面添加了一个下拉列表。一切正常,但当我尝试获取该下拉列表的选定值时,在浏览器控制台上出现错误“无法读取未定义的属性'undefined'”

代码:

var selected_index_book = document.getElementById("surah_selection");
var book_sel = selected_index_book.options[selected_index_book.selectedIndex].value;
var book_option_id=$('option:selected', $('#surah_selection')).index();
您可以尝试以下方法:

   var temp = $('#surah_selection option:selected').text();
使用


为什么不使用简单的方法获取所选值?我们可以看到您用于创建
#surah_selection
元素的代码吗?您可以显示html吗?
var book_sel = $("#surah_selection option:selected").text();
var book_option_id = $("#surah_selection option:selected").attr('id');