简体   繁体   English

我如何应用Onselect方法在javascript中下拉菜单

[英]how do i apply Onselect method to drop down menu in javascript

i am using twitter bootstrap to create a drop down menu, the menu contains 2 sub-menus as shown: 我正在使用twitter bootstrap创建一个下拉菜单,该菜单包含2个子菜单​​,如下所示:

    <div class="Menu"">
    <div class="btn-group">
        <button type="button" id="someaction" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
            Select Action
  <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
            <li><a class="add">Add</a></li>
            <li><a class="remove">Remove</a></li>
        </ul>
    </div>
</div>

Using JavaScript how do i add actions to each sub menus, so for example when i select Add it should alert me "Add" and when i click remove it should alert me "Remove". 使用JavaScript如何将操作添加到每个子菜单,例如,当我选择“添加”时,它会提醒我“添加”,而当我单击“删除”时,它会提醒我“删除”。 I think if loop will do a job for me, for example if (Add is selected do this else if Remove is selected do something else .. i would like to use this to perform different actions based on sub menus 我认为if循环会为我做一个工作,例如,如果(选择“添加”,则执行此操作;如果选择“删除”,则进行其他操作。

  $(".dropdown-menu li a").click(function () {

    });

The selector that you are using is perfectly alright, But since you are adding a click event to an anchor tag, You can optionally use .preventDefault() to prevent the page redirecting, 您使用的selector是完全可以的,但是由于您要向anchor标记添加click事件,因此可以选择使用.preventDefault()来防止页面重定向,

Just try, 你试一试,

$(".dropdown-menu li a").click(function (e) {
    e.preventDefault();
    alert($(this).text());
});

Note: you have to use .text() method to retrieve the text inside a tag. 注意:您必须使用.text()方法来检索标签内的文本。

DEMO 演示

Simply write the following code inside your function 只需在函数内部编写以下代码

alert($(this).text());// returns the current element text

Example Fiddle 小提琴的例子

You can refer to the fiddle http://jsfiddle.net/9GZQ2/ and can do what you want to do. 您可以参考小提琴http://jsfiddle.net/9GZQ2/并可以做您想做的事情。 All you need to do is to change the tasks inside the function for you it will be: alert($(this).text()); 您需要做的就是为您更改函数内的任务,即是:alert($(this).text());

$(function(){
    $("#ProducterType").change(function(){
       alert($(this).text());
    });
    $("#Role").change(function(){
       alert($(this).text());
    });
});

暂无
暂无

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

相关问题 如何使用 JavaScript 制作动态下拉菜单 - How do I make a dynamic drop down menu using JavaScript 如何使用“发布”方法将所有选定的数据从下拉菜单发送到服务器? - How do I send all selected data from the drop-down menu to the server with a “post” method? 如何使用Javascript确保下拉菜单中选择的所有选项均显示第一列? - How do I use Javascript to ensure that the first column appears for all the options selected in the drop-down menu? 如何将字符串数组转换为Rails / JavaScript文件中的选择下拉菜单? - How do I turn an array of strings into a select drop down menu in a Rails/JavaScript file? 如何在html / javascript的下拉菜单中基于用户选项更改变量 - How do I change a variable based on the users option from a drop down menu in html/javascript Excel VBA - 如何使用 Selenium (Javascript) 通过下拉菜单移动? - Excel VBA - How do I move by a drop-down menu with Selenium (Javascript)? 如何使用javascript获取下拉菜单列表中某项的值? - How do I get the value of an item in a drop down menu list using javascript? 如何从document.getElementById javascript制作下拉菜单 - How do I make drop down menu from document.getElementById javascript 如何关闭下拉菜单,因为同一个 class 中的另一个菜单在 jquery / javascript 中打开? - How do I make a drop down menu close as another from the same class opens in jquery / javascript? 当点击它之外的任何区域时,如何使用javascript / jquery隐藏此下拉菜单? - How do I hide this drop down menu using javascript/jquery when any area outside of it is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM