在选择框(下拉列表)中检查元素 ID 值

check element id-value in selectBox (dropdown)

本文关键字:检查 元素 ID 选择 下拉列表      更新时间:2023-09-26

我想检查我的选择框是否有id。

这是我的代码:

<select name="startTime" id="startTime">
    <option>23:00</option>
    <option id="nextDay">00:30</option>
</select>

现在我想检查用户是否正常(23:00)。或者id="nextDay"的那个。

所以这将是这样的检查:

if(selected has id="nextDay"){ //Than do something}

如何使用javascript和/或jquery来做到这一点?这是我想要的简化版本。

$(document).ready(function(){
    $("#startTime").change(function(){ // this can also be a form submit or something
        if($("#nextDay").is(":selected"))
        {
            alert("Working!");
                    //do something
        }
    });
});

这应该;)有效!对于以前的解决方案,代码是在加载页面时执行的,而不是在进行选择时执行的。