如何在输入字段中填充所选选项

时间:2019-02-11 07:41:49

标签: javascript jquery

我有一个带class="input-group"的div容器,我的输入字段带有下拉列表,该字段是我在bootstrap4的帮助下创建的 我想做的是,当用户单击任何下拉列表时,所选选项应该填充到相应的输入字段中

代码段

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<div class="container">
  <form>
    <hr>
    <div class="form-row">
      <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
        <label for="itemCode">Item Code</label>
        <div class="input-group">
          <input type="text" class="form-control" aria-label="Text input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
            <div class="dropdown-menu dropdown-menu-right">
              <a class="dropdown-item" href="#">Item Code 1</a>
              <a class="dropdown-item" href="#">Item Code 2</a>
              <a class="dropdown-item" href="#">Item Code 3</a>
            </div>
          </div>
        </div>
      </div>
      <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
        <label for="brandCode">Brand Code</label>
        <div class="input-group">
          <input type="text" class="form-control" aria-label="Text input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
            <div class="dropdown-menu dropdown-menu-right">
              <a class="dropdown-item" href="#">Brand Code 1</a>
              <a class="dropdown-item" href="#">Brand Code 2</a>
              <a class="dropdown-item" href="#">Brand Code 3</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>

我尝试了onclick事件,但是id不显示任何值

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

$(".dropdown-item").click(function() {
  $(this).closest(".input-group").find("input").val($(this).text())
});

工作演示

$(".dropdown-item").click(function() {
  $(this).closest(".input-group").find("input").val($(this).text())
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<div class="container">
  <form>
    <hr>
    <div class="form-row">
      <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
        <label for="itemCode">Item Code</label>
        <div class="input-group">
          <input type="text" class="form-control" aria-label="Text input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
            <div class="dropdown-menu dropdown-menu-right">
              <a class="dropdown-item" href="#">Item Code 1</a>
              <a class="dropdown-item" href="#">Item Code 2</a>
              <a class="dropdown-item" href="#">Item Code 3</a>
            </div>
          </div>
        </div>
      </div>
      <div class="form-group col-xs-6 col-sm-6 col-md-6 col-lg-3">
        <label for="brandCode">Brand Code</label>
        <div class="input-group">
          <input type="text" class="form-control" aria-label="Text input with dropdown button">
          <div class="input-group-append">
            <button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
            <div class="dropdown-menu dropdown-menu-right">
              <a class="dropdown-item" href="#">Brand Code 1</a>
              <a class="dropdown-item" href="#">Brand Code 2</a>
              <a class="dropdown-item" href="#">Brand Code 3</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>