如何在模态关闭时将选择框设置为默认值?

时间:2017-05-30 16:02:59

标签: javascript jquery html

当我隐藏模态时,我试图清除模态中的值。我能够清除常规输入字段的值,但无法使选择框返回到默认值(在这种情况下为' 0')。

这是我的HTML /模式:

<div class="modal fade" id="entity_request_modal" tabindex="-1" role="dialog" aria-labelledby="entity_request_modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Request to Add Business Entity</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    {% csrf_token %}

                    <label>Business Name</label>
                    <input id="entity_name" class="form-control" placeholder="Enter business name..." name="entity_name" value="">
                </div>
                <div class="form-group">
                    <label>Entity Type</label>
                    <select data-placeholder="Entity Type..." class="chosen-select" tabindex="-1" id="select_entity_type" name="select_entity_type">
                        <option value="0">Make a Selection</option>
                        <option value="3">Financial Institution</option>
                        <option value="14">College Institution</option>
                        <option value="13">Hospital or Clinic</option>
                        <option value="4">Internet Service Provider</option>
                        <option value="9">Cellular Provider</option>
                        <option value="10">Social Network</option>
                        <option value="7">Consumer Retailer</option>
                        <option value="8">Health Insurance Provider</option>
                        <option value="11">Auto Insurance Provider</option>
                        <option value="12">Home Insurance Provider</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" id="request_entity" class="btn btn-primary">
                    Save changes
                </button>
            </div>
        </div>
    </div>
</div>

这是我的JS / JQ:

$('#entity_request_modal').on('hide.bs.modal', function (e) {
    $('#entity_name').val('');
    $('#select_entity_type').val('0');
});

再次,&#39; #entity_name&#39;正在重置,但是#select_entity_type&#39;不是。想法?

1 个答案:

答案 0 :(得分:1)

您可以将选择选项selectedIndex prop设置为0;

$('#select_entity_type').prop('selectedIndex',0);

使用如下。

$('#entity_request_modal').on('hide.bs.modal', function (e) {
$('#entity_name').val('');
$('#select_entity_type').prop('selectedIndex',0);
});

同时检查Fiddle