1

I am trying to show the value of the textboxes in the select. For example, if you type anything in the Chairperson, President or VicePresident textfields the value of those textboxes should be displayed separately as options in the select.

I tried the below but it doesn't work.

$(document).ready(function() {
  $("#Chairperson_change").change(function() {
    document.getElementById("dropdown_change").value = document.getElementById("Signatories").value;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Chairperson: <input name="Chairperson" type="text" maxlength="50" id="Chairperson_change" style="width: 665px; padding:0px; z-index: 2; position: absolute;" /><br> 
President: <input name="President" type="text" maxlength="50" style="width: 665px; padding:0px; z-index: 2; position: absolute;"
/><br> 
VicePresident: <input name="VicePresident" type="text" maxlength="50" style="width: 665px; padding:0px; z-index: 2; position: absolute;" /><br>

<select id="dropdown_change"><br>
  <option value="">Options here</option>
</select>

4

1 回答 1

3

为此,您可以挂钩到input文本框的事件。然后,您可以为每个文本框创建新option元素(基于其id),或者如果它已经存在则更新一个。尝试这个:

$(function() {
  $(".position").on('input', function() {
    var $option = $(`#dropdown_change option[data-pos="${this.id}"]`);
    if (!$option.length)
      $option = $(`<option data-pos="${this.id}" />`);

    $option.text(this.value).val(this.value).appendTo('#dropdown_change');
  });
});
.position {
  width: 665px;
  padding: 0px;
  z-index: 2;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Chairperson: <input name="Chairperson" id="chair" type="text" maxlength="50" class="position" /><br /> 
President: <input name="President" id="pres" type="text" maxlength="50" class="position" /><br /> 
VicePresident: <input name="VicePresident" id="vice" type="text" maxlength="50" class="position" /><br />

<select id="dropdown_change">
  <option value="">Options here</option>
</select>

于 2018-07-13T13:39:01.877 回答