如何在交换时做多个选定值警报?

问题描述 投票:0回答:1

我想在数据库中从右到左记录信息,反之亦然

this.init = function() {
    var pickListHtml =
      "<div class='row'>" +
      "  <div class='col-sm-5'>" +
      " <h1>Atanmamis Kullanicilar</h1>" +
      "	 <select class='form-control pickListSelect pickData' multiple><option data-id='2'>aziz</option></select>" +
      " </div>" +
      " <div class='col-sm-2 pickListButtons'>" +
      "	<button type='button' class='pAdd btn btn-primary btn-sm'>" + opts.add + "</button>" +
      "      <button type='button'  class='pAddAll btn btn-primary btn-sm'>" + opts.remove + "</button>" +
      "	<button type='button' class='pRemove btn btn-primary btn-sm'>" + opts.addAll + "</button>" +
      "	<button type='button' class='pRemoveAll btn btn-primary btn-sm'>" + opts.removeAll + "</button>" +
      " </div>" +
      " <div class='col-sm-5'>" +
      " <h4>'Atanmis Kullanicilar'</h4>" +
      "    <select onchange='tester()' class='form-control pickListSelect pickListResult' id='pickListResult' multiple><option
    data - id = '22' > kartas < /option></select > " +
    " </div>" +
    "</div>";
    this.html(pickListHtml);

我正在添加jQuery但是如何编写一个方法来改变它?

function tester() {
  select = $('#pickListResult');
  selected = new Array();
  ////////i have no idea
  alert(selected);
}

JSFiddle

jquery html
1个回答
0
投票

它有点不清楚你想要你的selected包含什么,但你可以这样做。

function tester(obj) {
  var select = $(obj); // select is now equal to the select your changed.

  selected = select.find(":selected").toArray() // will return the options you have selected

  console.log(selected);
}

这将返回您在选择中选择的元素

注意让它工作你的onchange需要thisonchange='tester(this)'

演示

function tester(obj) {
  var select = $(obj);

  var selected = select.find(":selected").toArray()

  console.log(selected);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select onchange='tester(this)' class='form-control pickListSelect pickListResult' multiple>
  <option>test1</option>
  <option>test2</option>
  <option>test3</option>
  <option>test4</option>
</select>
© www.soinside.com 2019 - 2024. All rights reserved.