获取 MagicSuggest 输入的值

Get value of MagicSuggest input

我有一个具有一定价值的输入,我试图获得它的价值。当我点击 'valid' 按钮时,它显示 'undefined'。这是我的 jsfiddle:http://jsfiddle.net/pv2we0x3/1/

$(function() {
  $('.suggest-tag').magicSuggest({
    data: [{
      "Id": 1,
      "Name": "hello"
    }, {
      "Id": 2,
      "Name": "world"
    }, {
      "Id": 3,
      "Name": "no"
    }],
    valueField: 'Id',
    displayField: 'Name',
  });

  $('.btn').click(function() {
    alert($('.suggest-tag').serialize());
  })
});

根据 documentation for MagicSuggest,您需要在您创建的库实例上调用 getValue() 以获得选定的标签值:

$(function() {
  var ms = $('.suggest-tag').magicSuggest({
    data: [{
      "Id": 1,
      "Name": "hello"
    }, {
      "Id": 2,
      "Name": "world"
    }, {
      "Id": 3,
      "Name": "no"
    }],
    valueField: 'Id',
    displayField: 'Name',
  });

  $('.btn').click(function() {
    console.log(ms.getValue());
  })
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/magicsuggest/2.1.4/magicsuggest.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magicsuggest/2.1.4/magicsuggest.js"></script>

<form action="subscribe.php" method="post">
  <div class="form-group">
    <label>Tags</label>
    <div class="suggest-tag" value="[1,2,3]"></div>
  </div>
</form>

<input type="button" class="btn" value="Valid" />