gpt4 book ai didi

javascript - 更改事件不更新选择值

转载 作者:行者123 更新时间:2023-12-03 02:22:51 26 4
gpt4 key购买 nike

我正在使用一个运行良好的更改事件,除了它没有更改我选择元素中的 val() 值这一事实。我有 2 个选择希望在此更改事件上更新,以便值变为 val('')。但是,它正在使用数据更改#dstrdept选择,但完全忽略选择#address,#box_dstr

我不明白为什么它没有更新这些选择中的值,如果有人能指出我的错误,我将不胜感激。非常感谢

我还没有发布 sql,但假设所有选择都填充了数据。

已解决:添加了$("#address").trigger("chosen:updated");

html

<div class="form-group">
<label for="rtvcompany" class="labelStyle">Company</label>
<select class="form-control chosen-select" id="dstrcompany" name="dstrcompany" data-placeholder="Select a Company...">
<option value=""></option>
<?php
while ($row = mysqli_fetch_assoc($resultcmp)) {
$name = $row["idcode_usr"];
echo "<option value=\"$name\">$name</option>";

}
?>
</select>
<div id="compmessage"></div>
<div class="servicesHelp">
<lead id="serviceHelp" class="form-text text-muted">Please select a company to proceed.</lead></div>
</div>
<div class="form-group">
<label for="rtvdept" class="labelStyle">Department</label>
<select class="form-control chosen-select" id="dstrdept" name="dstrdept" data-placeholder="No departments to display...">
<option value=""></option>

</select>
<div id="deptmessage"></div>
<div class="servicesHelp">
<lead id="serviceHelp" class="form-text text-muted">Please select a department where your box is stored for destruction.</lead></div>
</div>

<div class="form-group">
<label for="address" class="labelStyle">Destruction Address</label>
<select class="form-control chosen-select" name="address" id="address" data-placeholder="No address to display...">
<option value=""></option>
</select>
<div id="addrmessage"></div>
<div class="servicesHelp"><lead id="addressHelp" class="form-text text-muted">Please select an address for the box destruction</lead>
</div>
</div>

<div class="form-group">
<label for="box_rtv" class="labelStyle">Select Your Box(es)</label>
<select class="form-control chosen-select" name="box_dstr[]" id="box_dstr" multiple data-placeholder="No boxes to display...">
<option value=""></option>
</select>
<div id="dstrmessage"></div>
<div class="servicesHelp"><lead id="serviceHelp" class="form-text text-muted">Please select your boxes from the list. You can select a max of 3 boxes per submission. You can select multiple boxes by holding the left ctrl on your keyboard and making your selection</lead>
</div>
<div class="noBrtvBoxes" style="color:white;"></div>
</div>

js

$(function() {
$(document).on('change', '#dstrcompany', function() {
value = $(this).val();
$(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
$.get('/domain/admin/requests/boxes/destroy/loadboxDstrcompany.php?dstrcompany=' + value, function(data) {
//console.log(value);
$("#dstrdept").html(data);
$('#loader').slideUp(200, function() {
$(this).remove();
$("#dstrdept").val('');
$("#compmessage").hide();
$("#address").val('');
$("#address").attr("data-placeholder", "Select your address");
$("#box_dstr").val('');
$("#box_dstr").attr("data-placeholder", "Select your box");
//$("#rtvdept").attr("data-placeholder", "Select your new department");
// $("#rtvdept").data("chosen").default_text = "New Default Text"
$("#dstrdept").trigger("chosen:updated");
});
});
});
});

最佳答案

“select”元素中没有占位符,“data-placeholder”在这种情况下不起作用,需要添加一个“option”并设置其 value 属性。

用于地址下拉列表而不是此

 <select class="form-control chosen-select" name="address" id="address" data-placeholder="No address to display...">
<option value=""></option>
</select>

你可以使用

 <select class="form-control chosen-select" name="address" id="address" data-placeholder="No address to display...">
<option value="0">Select your address</option>
<option value="1">Addr1</option>
</select>

并设置值

$("#address").val(0);
//$("#address").attr("data-placeholder", "Select your address");

其他选择选项也类似

关于javascript - 更改事件不更新选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49082037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com