How to tell javascript x.value = $('‪#‎selectID‬ option:


How to tell javascript x.value = $('‪#‎selectID‬ option:selected').text(); that 'selectID' is not a name of an ID but rather a variable

我有一个显示一系列选定元素的while循环。

$i = 0;
while(condition)
{
    <input type = 'hidden' id = 'hide' value = ' '>
    <select id = '$i' onchange = 'gettext(this.id)'></select>
    <option></option>
    ...
    ....
    $i++;
}
function gettext(selectID)
{
    var x = document.getElementById('hide');
    x.value = $('‪#‎selectID‬ option:selected').text();
}

现在,我需要获取用户选择的选定文本。我应该如何告诉javascript/jquery'selectID'不是某个元素的id的名称 - selectID是一个变量,它保存了我想从中获取文本的选择元素的ID的值。

回答您的问题:

$('‪#' + ‎selectID‬ + ' option:selected').text();

只需将变量连接为名称的一部分即可。