更改事件未更新选择值

时间:2018-03-03 08:00:27

标签: javascript jquery

我正在使用一个工作正常的更改事件,除了它没有更改select元素中的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");
      });
    });
  });
});

1 个答案:

答案 0 :(得分:1)

没有占位符选择&#39;元素,&#34;数据占位符&#34;在这种情况下无法工作,需要添加一个选项&#39;并设置其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");

类似于其他选择选项