JavaScript 显示/隐藏字段不起作用 - OnChange 不会每次都发生


JavaScript Display/Hide Fields not working - OnChange doesn't occur everytime

I am trying to hide and show fields based of the selection in a dropdown field. I got it do hide but when I select another value it doesn't display again.
<script type="text/javacript">
        function checkEm(){
            if (document.getElementById('JobTypeDDL').selected = "2"){
                document.getElementById('DateTbx').style.visibility = "hidden";
                document.getElementById('DateTbx').style.display = "none";
            } else {
                document.getElementById('DateTbx').style.visibility = "visible";
                document.getElementById('DateTbx').style.display = "block";
        }
    }
</script>
    <div class="InputField">
        <select name="JobTypeDDL" id="JobTypeDDL" onChange="checkEm()">
            <option value="0" <?php if ($_POST['JobTypeDDL'] == "0") {echo "selected='selected'";} ?> >Select Job Type</option>
            <option value="1" <?php if ($_POST['JobTypeDDL'] == "1") {echo "selected='selected'";} ?> >New Mobility</option>
            <option value="2" <?php if ($_POST['JobTypeDDL'] == "2") {echo "selected='selected'";} ?> >Service/Repair/Install</option>
        </select>
    </div>
<input name="DateTbx" id="DateTbx" type="text" <?php if (!$_POST['DateTbx'] == "") {echo "value='". $_POST['DateTbx']."'";} ?> />

使用 ==if 语句中测试相等性。您正在使用=,这是赋值。

在 if 条件中,您使用单个 =。 请改用===