我有一个selectpicker下拉列表。我需要将选定的选项值附加到textarea。我希望每次在textarea中将选定的选项值附加到新行。当我取消选择该选项时,我希望从textarea中删除该值。
function getvalue(myname) {
alert(myname.toString());
if (document.getElementById(myname).selected){
arr.push(myname);
}
else
{
arr.splice( arr.indexOf(myname), 1 );
}
console.log(arr);
document.getElementById("field").value = arr.join("\n");
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.min.css" />
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<div class="form-group">
<p>Select Medicines<span>*</span></p>
<select class="selectpicker form-control" data-size="5" data-live-search="true" name = "med" id ="med" >
<option onclick="getvalue(this.id)" value="s1" id="s1" >Select</option>
<option onclick="getvalue(this.id)" id="s2" >Select1</option>
<option onclick="getvalue(this.id)" id="s3">Select2</option>
</select>
</div>
<textarea id="field"></textarea>