简体   繁体   English

引导 select 下拉“无结果”点击事件

[英]Bootstrap select dropdown "No Results" click event

I am using bootstrap select dropdown which has the following code.我正在使用引导程序 select 下拉菜单,其中包含以下代码。

HTML HTML


    @Html.DropDownList("Product", SelectList, new { data_live_search = "true", @class = "content-small selectpicker", @tabindex = 1 })

In this dropdown i need to provide an option to add new values which are typed in the search bar in the dropdown to the existing collection of the dropdown based on the click event.在此下拉列表中,我需要提供一个选项,以将在下拉列表中的搜索栏中键入的新值添加到基于单击事件的下拉列表的现有集合中。 I have modified the UI to say "Create" instead of No results found.我已将 UI 修改为“创建”而不是“未找到结果”。 Please refer the code and UI below.请参考下面的代码和 UI。

JS JS


    $('#Product').selectpicker({ noneResultsText: 'Create ' });

UI用户界面

创建选项的 UI

I am trying to add click event to this text "Create" content which is highlighted in the screenshot above to add that content dynamically to the existing collection of the dropdown.我正在尝试将点击事件添加到此文本“创建”内容中,该内容在上面的屏幕截图中突出显示,以将该内容动态添加到下拉列表的现有集合中。

But, while clicking that text the dropdown gets closed and the element which shows the text gets removed, hence i could not call the on click function to add this item dynamically.但是,在单击该文本时,下拉菜单会关闭并且显示文本的元素会被删除,因此我无法调用单击 function 来动态添加此项目。

Can anyone please let me know the possibilities of using this.?谁能让我知道使用它的可能性。?

Vijay.维杰。

I think you're skipping that the input you're dealing with is outside of the .selectpicker .我认为您正在跳过您正在处理的输入在.selectpicker之外。 You can find working snippet below.您可以在下面找到工作片段。

 $('.selectpicker').selectpicker({ noneResultsText: '<a href="#" class="creator">Create</a> ' }); $(".selectpicker").on("click",".creator",function(){ $("p span").html("Clicked for "+$(".pickercontainer.bs-searchbox input").val()); });
 .creator { display:inline-block; float:left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel = "stylesheet" type = "text/css" href = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" /> <link rel = "stylesheet" type = "text/css" href = "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.css"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.js"></script> <div class='pickercontainer'> <select class="selectpicker" data-live-search="true" > <option>Alabama</option> <option>Alaska </option> <option>Arizona </option> </select> </div> <br> <p>Action: <span></span></p>

$('.selectpicker').selectpicker({ noneResultsText: ' {0} (create new)' }); $('.selectpicker').selectpicker({ noneResultsText: ' {0} (create new)' });

function createNew(event){ var value = $(event).parents('div').siblings('.bs-searchbox').find('input').val(); function createNew(event){ var value = $(event).parents('div').siblings('.bs-searchbox').find('input').val(); //Do something.... alert(value); //做点什么... alert(value); } }

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

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