为什么设置了JS的功能后我的引导程序切换不变?

问题描述 投票:0回答:2

我想根据JS中的设置,将切换更改为打开或关闭。在这种情况下,我将JS中的设置为“无效”,因为当模态关闭时,HTML使用的是有效/无效值。如示例所示,我的下拉列表运行良好,一旦关闭模态,它将值更改为Standard。

enter image description here

HTML

<div class="form-group row">
    <label class="col-md-4">Status Device</label>
    <div class="col-md-8">
        <label class="switch">
            <input type="checkbox" id="toggle_pump">
            <div class="slider round">
                <span class="on" name="add_device_status" id="add_device_status" value="Active" autocomplete="off">ON</span>
                <span class="off" name="add_device_status" id="add_device_status" value="Inactive" autocomplete="off">OFF</span>
            </div>
        </label>
    </div>
</div>
<div class="form-group row">
    <label class="col-md-4">Device Rule</label>
    <div class="col-md-8">
        <div class="form-group">
            <select id="add_device_rule" class="custom-select form-control">
                <option value="std_pckg">Standard Package</option>
                <option value="vip_pckg">VIP Package</option>
            </select>
        </div>
    </div>
</div>

JS

$("#add_new_device_modal").on('hidden.bs.modal', function(e) {
    $('#add_device_status').val("Inactive");
    $('#add_device_rule').val("std_pckg");
});
javascript jquery html modal-dialog toggle
2个回答
0
投票

这是因为您在该部分声明了两个具有相同名称的ID。

<div class="slider round">
<span class="on" name="add_device_status" id="add_device_status" 
value="Active" autocomplete="off">ON</span>
<span class="off" name="add_device_status" id="add_device_status" 
value="Inactive" autocomplete="off">OFF</span>
</div>

将其更改为-

<div class="slider round">
<span class="off" name="add_device_status" id="add_device_status" 
value="Active" autocomplete="off">ON</span>
</div>

JS-

$("#add_new_device_modal").on('hidden.bs.modal', function(e) {
$('#add_device_status').val("Inactive");
$('#add_device_status').innerHTML = "OFF";
$('#add_device_rule').val("std_pckg");
});

0
投票

https://jsfiddle.net/w0aqvg5m/

使用选中的属性来切换模式隐藏事件

$('#toggle_pump').prop('checked',true)

© www.soinside.com 2019 - 2024. All rights reserved.