gpt4 book ai didi

javascript - Twitter Bootstrap 的事件处理程序下拉发送值?

转载 作者:行者123 更新时间:2023-11-30 17:44:23 26 4
gpt4 key购买 nike

告诉我如何传递从列表框中选择的值

JavaScript:

$('.dropdown-menu li').click(function(e){
e.preventDefault();
var selected = $(this).text();
$('.category').val(selected);
});

HTML

<div class="container">
<div class="col-sm-7 pull-right well">
<form class="form-inline" action="#" method="get">
<div class="input-group col-sm-8">
<input class="form-control" type="text" value="" placeholder="Search" name="q">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Action <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" id="action-1">1</a></li>
<li><a href="#" id="action-1">2</a></li>
<li><a href="#" id="action-1">3</a></li>
</ul>
<input type="hidden" name="category" class="category">
</div><!-- /btn-group -->
</div>
<button class="btn btn-primary col-sm-3 pull-right" type="submit">Search</button>
</form>
</div>
</div>

允许在弹窗中选择“1”传给类=“form-control”值=“1”

示例代码如下: http://bootply.com/93417

最佳答案

您将值放入名为类别的隐藏文本框中。如果您希望在搜索字段中添加一个新类(在本例中我添加了 box)到它的类属性并在您的脚本中调用它:

链接:http://bootply.com/98806

像这样:

<div class="container">
<div class="col-sm-7 pull-right well">
<form class="form-inline" action="#" method="get">
<div class="input-group col-sm-8">
<input class="form-control box" type="text" value="" placeholder="Search" name="q">
<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Select <span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
<input type="hidden" name="category" class="category">
</div><!-- /btn-group -->
</div>
<button class="btn btn-primary col-sm-3 pull-right" type="submit">Search</button>
</form>
</div>

并相应地改变你的脚本

$('.dropdown-menu li').click(function(e){
e.preventDefault();
var selected = $(this).text();
$('.box').val(selected);
});

关于javascript - Twitter Bootstrap 的事件处理程序下拉发送值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20408939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com