我想根据JS中的设置,将切换更改为打开或关闭。在这种情况下,我将JS
中的设置为“无效”,因为当模态关闭时,HTML
使用的是有效/无效值。如示例所示,我的下拉列表运行良好,一旦关闭模态,它将值更改为Standard。
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");
});
这是因为您在该部分声明了两个具有相同名称的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");
});