简体   繁体   English

ASP.NET Bootstrap下拉菜单-选择一个项目

[英]Asp.net bootstrap dropdown menu - selecting an item

I have an ASP.net project which used Bootstrap controls. 我有一个使用Bootstrap控件的ASP.net项目。 I have the following Bootstrap dropdown menu which pulls in the list items programmatically from a database. 我有以下Bootstrap下拉菜单,该菜单以编程方式从数据库中提取列表项。

However, when i click on the menu and select one of the items, i obviously need that item to show in the dropdown box. 但是,当我单击菜单并选择一项时,我显然需要该项显示在下拉框中。 Instead, when an item is clicked, nothing happens apart from the dropdown menu is closed. 而是,当单击一个项目时,除了关闭下拉菜单外,什么都没有发生。

<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" id="merchantList" data-toggle="dropdown" aria-expanded="false"> Choose Merchant <span class="caret"></span></button>
<ul class="dropdown-menu" role="menu" aria-labelledby="merchantList" id="myList" runat="server">
<asp:Literal runat="server" id="merchantDropDown"></asp:Literal>
</ul>
</div>

I'm assuming some kind of JavaScript is needed to set the dropdown menu to whichever item has been selected. 我假设需要某种JavaScript才能将下拉菜单设置为已选择的项目。 But i'm not sure how to do it. 但是我不确定该怎么做。

I've tried: 我试过了:

$("#myList li a").click(function () {
              alert('clicked');
              var selText = $(this).text();
              $(this).parents('.btn-group').find('.dropdown-toggle').html(selText + ' <span class="caret"></span>');

          });

also tried: 还尝试了:

$('#myList li a').on('click', function () {
              $('#merchantList').val($(this).text());

          });

But these never seems to get called. 但是这些似乎从未被调用过。 Any ideas? 有任何想法吗? It would be even better if I could do this in the code behind as I prefer to work back there. 如果我可以在后面的代码中这样做,那就更好了,因为我更喜欢在那儿工作。

Correct me if I am misunderstanding what you are looking to do but shouldn't you be able to find the item that they selected and set its css class="active". 如果我误解了您要做什么,请纠正我,但您是否不应该找到他们选择的项目并设置其css class =“ active”。 To figure out that the current item they selected matches the current page they are on there are several techniques using the URL. 为了弄清楚他们选择的当前项目是否与他们所在的当前页面匹配,有几种使用URL的技术。

Try doing something like the following on your master page: 尝试在母版页上执行以下操作:

var url = window.location;
$('ul.nav a[href="'+ url +'"]').parent().addClass('active');

I did it like this in the end and it seems to all be working. 我最终还是这样做的,似乎一切正常。 Simple enough i just couldn't figure it out as i'm a JS newbie! 很简单,因为我是JS新手,我无法弄清楚!

<script>

      $().ready(function () {
         $('a').click(function () {
           var value = $(this).text();
           $('#merchantList').html(value + ' <span class="caret"></span>');
           alert(value);
          });
       });

</script>

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

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