在更改事件上附加选定的插件占位符文本

时间:2018-02-22 12:16:29

标签: javascript jquery

我正在为我的select元素使用jquery选择的插件。除了我尝试的任何事情,我都无法在更改事件后修改占位符文本。如果有人能检查我的代码指出我的错误,我将不胜感激。非常感谢

HTML

<div class="form-group">
    <label for="rtvcompany" class="labelStyle">Company</label>
    <select class="form-control chosen-select" name="rtvcompany" id="rtvcompany" 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" name="rtvdept" id="rtvdept" data-placeholder="Select a Dept...">
      <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 retrieval.</lead></div>
  </div>

JS

$(function() {
  $(document).on('change', '#rtvcompany', function() {
    $(this).after('<div id="loader"><img src="/domain/admin/images/loader.gif" alt="loading files" /></div>');
    $.get('/domain/admin/requests/boxes/retrieve/loadboxRtvaddr.php?rtvcompany=' + $(this).val(), function(data) {
      //console.log(data);
      $("#rtvdept").html(data);
      $('#loader').slideUp(200, function() {
        $(this).remove();
        $('#rtvdept').attr('data-placeholder', 'Please select your department....');
       // $("#rtvdept").data("chosen").default_text = "New Default Text"
        $("#rtvdept").trigger("chosen:updated");
      });
    });
  });
});

1 个答案:

答案 0 :(得分:1)

您正在创建没有价值的选项,请更改此选项:

echo "<option>$name</option>";

到此:

echo "<option value=\"$name\">$name</option>";