如何从更改功能中获取JqueryUI Selectmenu中的选定值?

时间:2019-07-02 10:42:31

标签: javascript jquery jquery-ui jquery-ui-selectmenu

我需要在JqueryUI SelectMenu中从更改事件中获取选择的选项值,但是我无法从更改函数或selectmenu函数中获取值, 怎么做/

  var iSelectedValue;
  var map="";
    $("#drpRegionName").selectmenu({
        change: function (event, ui) {

           iSelectedValue = ui.item.value;

            console.log(iSelectedValue); //Working
        },
        select: function (event, ui) {
            map = $(this).val();
            console.log(map); //working
        }
    });
    console.log(map); // not working
    console.log($(iSelectedValue); // not working

函数未定义。

1 个答案:

答案 0 :(得分:0)

考虑以下基于https://jqueryui.com/selectmenu/#product-selection

的示例

$(function() {
  var iSelectedValue;
  var map = "";

  function getValues(p) {
    var arr = [];
    $("select", p).each(function(i, el) {
      arr.push({
        id: $(el).attr("id"),
        value: $(el).val()
      });
    });
    return arr;
  }

  $("fieldset select").selectmenu({
    change: function(e, ui) {
      iSelectedValue = getValues($("fieldset"));
      console.log(iSelectedValue);
    }
  });

  iSelectedValue = getValues($("fieldset"));
  console.log(iSelectedValue);
});
fieldset {
  border: 0;
}

label {
  display: block;
  margin: 30px 0 0 0;
}

.overflow {
  height: 200px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<fieldset>
  <label for="speed">Select a speed</label>
  <select name="speed" id="speed">
    <option>Slower</option>
    <option>Slow</option>
    <option selected="selected">Medium</option>
    <option>Fast</option>
    <option>Faster</option>
  </select>

  <label for="files">Select a file</label>
  <select name="files" id="files">
    <optgroup label="Scripts">
      <option value="jquery">jQuery.js</option>
      <option value="jqueryui">ui.jQuery.js</option>
    </optgroup>
    <optgroup label="Other files">
      <option value="somefile">Some unknown file</option>
      <option value="someotherfile">Some other file with a very long option text</option>
    </optgroup>
  </select>

  <label for="number">Select a number</label>
  <select name="number" id="number">
    <option>1</option>
    <option selected="selected">2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
    <option>16</option>
    <option>17</option>
    <option>18</option>
    <option>19</option>
  </select>

  <label for="salutation">Select a title</label>
  <select name="salutation" id="salutation">
    <option disabled selected>Please pick one</option>
    <option>Mr.</option>
    <option>Mrs.</option>
    <option>Dr.</option>
    <option>Prof.</option>
    <option>Other</option>
  </select>
</fieldset>

您可以在此处看到如何在页面加载时以及选择菜单项之一发生change事件时更新数组。

希望有帮助。