更改时间后更改下拉菜单

时间:2019-07-12 08:58:53

标签: javascript php drop-down-menu

我编写了一个php网站/ javascript函数,用于在时间介于例如7.00-8.00(id530)或13-15(id1330)或20-03(id2130)。如果时间为7.31,则下拉菜单应显示网站的负载/将其负载更改为id530。

我试图将时间与strtotime进行比较,但是我不确定如何获取 这在javascript函数中。

<select id="soa" name="soa" required style="width: 154px" 
onload="var  selectBoxField = document.getElementById('soa')
var fieldNumber = document.getElementById('$time')
fieldNumber.addEventListener('keyup', ({target}) => {  
var index = Array.from(selectBoxField).findIndex(option => option.id === target.value);
selectBoxField.selectedIndex = index;
})">
            <option id="530" value="1">s1</option>
            <option id="1330" value="2">s2</option>
            <option id="2130" value="3">s3/option>
            </select>

1 个答案:

答案 0 :(得分:0)

在将整个窗口(包括$ time元素)确定加载之后,请尝试将代码分别放入元素的onload函数中,而不是将其触发:

<script>
        window.addEventListener('load', (event) => {
            var  selectBoxField = document.getElementById('soa');
            var fieldNumber = document.getElementById('$time');
            fieldNumber.addEventListener('keyup', ({target}) => {  
                var index = Array.from(selectBoxField).findIndex(option => option.id === target.value);
                selectBoxField.selectedIndex = index;
            });
        });
</script>

如果问题仍然存在,请编辑您的帖子,使其也包含$ time元素,以便我进行全面测试。

编辑:请注意,您的第三个选项没有正确的结束标记:/option>

相关问题