简体   繁体   English

使用jQuery在输入框中显示所选文本

[英]selected text to display in input box using jquery

I have a multiple select dropdown using selectpicker and i want to display the selected text in another input box with @selected name not the value. 我有一个使用selectpicker的多选下拉列表,我想在另一个输入框中以@selected名称而不是值显示所选文本。 在此处输入图片说明 In this on click of plus a dropdown should open and after selecting the plus remains but the selected text has to display in another input box. 单击加号后,应该会打开一个下拉列表,选择加号后仍会保留,但所选文本必须显示在另一个输入框中。 here is the code that i have tried HTML code 这是我尝试过的HTML代码

 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css" />
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script> 


 <select id='recruiter' name="recruiter"  class="selectpicker" multiple>
     <optgroup label="Camping">
    <option value="1">Tent</option>
    <option value="2">Flashlight</option>
    <option value="3">Toilet Paper</option>
  </optgroup>
    <optgroup label="fruits">
    <option value="6">apple</option>
    <option value="7">orange</option>
    <option value="8">banana</option>
  </optgroup>
  </select>
 <input type="text" class="width-100 chat-borderTB pad-5" placeholder="comment" aria-describedby="sizing-addon2" name="msg" id="msg">


  <script type="text/javascript">
         $("#recruiter").on("change",function(){

           update();
         });

         function update() {

var selected=[];
 $('#recruiter :selected').each(function(){
     selected[$(this).val()]=$(this).text();
    });
 // alert($.trim(selected));
console.log(selected);
         $("#msg").val(selected);
         }
      </script>

Can u suggest how to do this. 你能建议如何做到这一点。

Try this code: 试试这个代码:

var selected=[];
 $('#recruiter option:selected').each(function(){
     selected[$(this).val()]=$.trim($(this).text());
    });
console.log(selected);
         $("#msg").val(selected);
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM