尝试使用 ASP-classic 和 jQuery 根据数据库的结果自动选择下拉选项

Trying to automatically select a dropdown option based off of a result from a database using ASP-classic and jQuery

本文关键字:结果 选择 选项 数据库 ASP-classic jQuery      更新时间:2023-09-26

我有一个选项选择下拉列表,使用asp-classic从oracle数据库中提取的数据自动填充。 该选项的 ID 是员工 ID 号。 我正在对数据库进行查询,以获取在轮班期间工作的员工 ID。获得该 ID 后,我想将该选项设置为当前选择的选项,但它对我不起作用。 这是我得到的:

<select id="supervisor1" style="width:90%">
   <option value="1234" id="1234">John1           Smith                </option>
   <option value="1235" id="1235">John2           Smith                </option>
   <option value="1236" id="1236">John3           Smith                </option>
   <option value="1237" id="1237">John4           Smith                </option>
   <option value="1345" id="1345">John5           Smith                </option>
   <option value="1346" id="1346">James          Smith                 </option>
</select>

这就是选择的一个例子。 显然,姓名和身份证号码已经更改,实际程序中大约有50个名字。

现在,当 ASP 代码在页面加载时执行时,当前主管存储在名为"foreman1"的 ASP 变量中。

<script>
    var selectedIndex = '<%=foreman1%>'
    var sup1 = document.getElementById('supervisor1');
    //alert(selectedIndex);
    sup1.options[sup1.options.selectedIndex].selected = true;
</script>

这将 'foreman1' 的值存储在 javascript 变量中。 它也将主管选择框存储在变量中。 然后它应该通过 supervisor1 的选项,找到 id 为 'foreman1' 的那个,然后让它被选中,我想这会让它在页面上显示为被选中的那个。 但是,它没有这样做。 我做错了什么?

如果selectedIndex是您要选择的选项的值,只需将选择值设置为相同的值

<script>
    var selectedIndex = '<%=foreman1%>'
    var sup1 = document.getElementById('supervisor1');
    sup1.value = selectedIndex;
</script>

并将脚本标记放在</body>之前