输入字段应该隐藏,当选择值是'其他'

时间:2015-04-20 14:16:25

标签: javascript php jquery html

我试图隐藏输入字段(id:mopothers)当select(id:mop)值不是'其他'但输入字段不隐藏

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

<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>

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

1 个答案:

答案 0 :(得分:1)

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

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

DEMO