输入字段应在选择值'others'时隐藏



>im 当选择 (id: mop) 值不是"其他"但输入字段不隐藏时尝试隐藏输入字段 (id: mopothers)

脚本-----------

<script type="text/javascript">
   $('select[name=mop]').change(function () {
        if ($(this).val() == '5') {
            $('#mopothers').show();
        } else {
            $('#mopothers').hide();
        }
    });
</script>

.html-------

<div data-row-span="2">
            <div data-field-span="1">
                <label>
                    Mode of Payment
                </label>
                <select id = "mop" name = "mop">
                    <option value="">Select mode of payment</option>
                    <option value="1">Cash</option>
                    <option value="2">Check</option>
                    <option value="3">Card</option>
                    <option value="4" >Bank Transfer</option>
                    <option value="5">Others</option>
                </select>
            </div>
            <div data-field-span="1">
                    <label>
                        <?php
                            if(isset($_SESSION['mopothersrequired'])) {
                                echo '<label style="color: red; ">
                                        *Mode of Payment (Others) is Required
                                         </label>';
                                unset($_SESSION['mopothersrequired']);  
                            }
                            else echo '<label>
                                        Mode of Payment (Others)
                                         </label>';  
                        ?>
                    </label>
                <input type="text" id = "mopothers" name = "mopothers">
            </div>
        </div>

一旦用户选择其他人,输入字段就会出现,一旦他选择现金、支票、卡、银行或支票,就会隐藏输入字段

尝试在

初始阶段调用一次change事件,

$('select[name=mop]').change(function () {
   $('#mopothers').toggle($(this).val() === '5');
}).change();

演示

最新更新