简体   繁体   English

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

[英]how to populate selected option inside a input field

I have div container with class="input-group" i am having input field with drop down, which i am creating with the help of bootstrap4 what i am trying to do is when user clicks on any dropdown the selected option should gets populate inside that respective input field 我有带有class="input-group" div容器,我具有带下拉列表的输入字段,该字段是我在bootstrap4的帮助下创建的,当用户单击任何下拉列表时,所选选项应该填充到其中相应的输入字段

Code Snippet 代码段

 <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> 

i have tried the onclick event but id dosn't print any values 我已经尝试过onclick事件,但是id不会显示任何值

How can i do this is there any easy way 有什么简单的方法我该怎么做

You can use the following code: 您可以使用以下代码:

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

Working demo 工作演示

 $(".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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM