Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the values of multiple choose dropdown box

How i can get the value of this multiple choose dropdown box? I cant figure out how i can get the values. Please help on this. Thanks!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.rawgit.com/harvesthq/chosen/gh-pages/chosen.jquery.min.js"></script>
    <link href="https://cdn.rawgit.com/harvesthq/chosen/gh-pages/chosen.min.css" rel="stylesheet"/>
  </head>
  <body>
      <select data-placeholder="Begin typing a name to filter..." multiple class="chosen-select" name="test" id="test">
        <option value=""></option>
        <option>American Black Bear</option>
        <option>Asiatic Black Bear</option>
        <option>Brown Bear</option>
        <option>Giant Panda</option>
        <option>Sloth Bear</option>
        <option>Sun Bear</option>
        <option>Polar Bear</option>
        <option>Spectacled Bear</option>
      </select>
      <input type="text" name="getvalue" id="getvalue">
      <input type="button" onclick="a()" value="Submit">
    <script>
    $(".chosen-select").chosen({
      no_results_text: "Oops, nothing found!"
    });

    function a() {
      var value = document.getElementById('test').value;
      document.getElementById('getvalue').value = value;
    }
    </script>
like image 260
MOO Avatar asked Sep 17 '25 07:09

MOO


2 Answers

try this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.rawgit.com/harvesthq/chosen/gh-pages/chosen.jquery.min.js"></script>
    <link href="https://cdn.rawgit.com/harvesthq/chosen/gh-pages/chosen.min.css" rel="stylesheet"/>
  </head>
  <body>
      <select data-placeholder="Begin typing a name to filter..." multiple class="chosen-select" name="test" id="test">
        <option value=""></option>
        <option>American Black Bear</option>
        <option>Asiatic Black Bear</option>
        <option>Brown Bear</option>
        <option>Giant Panda</option>
        <option>Sloth Bear</option>
        <option>Sun Bear</option>
        <option>Polar Bear</option>
        <option>Spectacled Bear</option>
      </select>
      <input type="text" name="getvalue" id="getvalue">
      <input type="button" onclick="a()" value="Submit">
    <script>
    $(".chosen-select").chosen({
      no_results_text: "Oops, nothing found!"
    });

    function a() {
      var value = $('#test').val();
      $('#getvalue').val(value);
    }
    </script>
like image 50
JoinCompany Avatar answered Sep 19 '25 20:09

JoinCompany


JsFiddle Link

    $(".chosen-select").chosen({
      no_results_text: "Oops, nothing found!"
    });

    $("button").click(function() {
      a = $(".chosen-select").val();
      console.log(a);
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://combinatronics.com/harvesthq/chosen/gh-pages/chosen.jquery.min.js"></script>
<link href="https://combinatronics.com/harvesthq/chosen/gh-pages/chosen.min.css" rel="stylesheet" />

<body>
  <select data-placeholder="Begin typing a name to filter..." multiple class="chosen-select" name="test" id="test">
        <option value=""></option>
        <option>American Black Bear</option>
        <option>Asiatic Black Bear</option>
        <option>Brown Bear</option>
        <option>Giant Panda</option>
        <option>Sloth Bear</option>
        <option>Sun Bear</option>
        <option>Polar Bear</option>
        <option>Spectacled Bear</option>
      </select>
  <input type="text" name="getvalue" id="getvalue">
  <input type="button" onclick="a()" value="Submit">
  <button>
click me
</button>
like image 42
Munkhdelger Tumenbayar Avatar answered Sep 19 '25 19:09

Munkhdelger Tumenbayar