简体   繁体   English

如何使用JavaScript获取按钮的选定值

[英]How to fetch selected value of button using javascript

I have following code for search bar where there is a Drop down list which I have developed on button and a text field to type search query. 我在搜索栏上有以下代码,其中有一个在按钮上开发的下拉列表和一个用于键入搜索查询的文本字段。

On button click I am retrieving text using 在按钮上单击我正在使用检索文本

document.getElementById('').value;

But how can fetch selected text of drop down button? 但是如何获取下拉按钮的选定文本?

My code : 我的代码:

<div class="input-group">
    <div class="input-group-btn search-panel">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" >
            <span id="search_concept">T Shirts</span> <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu">
            <li><a href="#its_equal">Watches</a></li>
        </ul>
    </div>
    <input type="hidden" name="search_param" value="all" id="search_param">
    <input type="text" class="form-control" id="niket" placeholder="Search term...">
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" id="apply"><span class="glyphicon glyphicon-search"></span>
        </button>
    </span>
</div>

It looks like you're using Bootstrap. 看来您正在使用Bootstrap。 Bootstrap components often require jQuery. 自举组件通常需要jQuery。 Use jQuery to bind a click handler to each of the dropdown items you want to retrieve a value from, in this case the <a> tags. 使用jQuery将点击处理程序绑定到要从中检索值的每个下拉项(在本例中为<a>标记)。

You can store that value in a variable and retrieve it when the search button is click. 您可以将该值存储在变量中,并在单击搜索按钮时检索它。

 (function(window, document, $, undefined) { var dropdownValue; /* Search dropdown menu for items that hold values we want, ".dropdown-menu a" */ $('.dropdown-menu a').each(function(i, item) { /* Add click handler to each one, whenever one is clicked the dropdownValue variable is updated with it's value */ $(this).on('click', function(e) { dropdownValue = this.innerText; }); }); /* Retrieve dropdown value */ $('#apply').on('click', function(e) { alert(dropdownValue); }); }(window, window.document, jQuery)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="input-group"> <div class="input-group-btn search-panel"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> <span id="search_concept">T Shirts</span> <span class="caret"></span> </button> <ul class="dropdown-menu" role="menu"> <li><a href="#its_equal">Watches</a> </li> <li><a href="#its_equal">Ties</a> </li> </ul> </div> <input type="hidden" name="search_param" value="all" id="search_param"> <input type="text" class="form-control" id="niket" placeholder="Search term..."> <span class="input-group-btn"> <button class="btn btn-default" type="button" id="apply"><span class="glyphicon glyphicon-search"></span> </button> </span> </div> 

Another way would be to apply a class to the selected <a> tag and query for that when the search button is clicked so you don't have to create a variable and update it. 另一种方法是将一个类应用于所选的<a>标记,并在单击搜索按钮时对其进行查询,因此您不必创建变量并对其进行更新。

暂无
暂无

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

相关问题 如何使用JavaScript将多个下拉列表选择的值提取到单个数组中 - How to fetch multiple dropdowns selected value into a single array using JavaScript 如何通过使用javascript单击按钮来获取特定行列的值 - how to fetch value of particular row column by clicking button using javascript 无法使用JavaScript在下拉框中获取选定的值 - Cannot fetch the selected value in a drop down box using JavaScript 如何检查是否使用JavaScript选择了单选按钮 - How to check if radio button is selected using javascript 如何使用javascript获取for循环中的最后一个值? - How to fetch last value in the for loop using javascript? 如何使用javascript获取无序列表中单选按钮的选定索引和值 - How to get the Selected index and value of a radio button inside an unorderd list using javascript 如何使用 javascript 获取多个单选按钮具有相同名称的选定单选按钮的值 - How to get the value of selected radio button where multiple radio buttons have same name using javascript 如何使用JavaScript将单选按钮的选定值从弹出窗口传递到父窗口? - How to pass the selected value of radio button using JavaScript from popup window to parent window? 在JavaScript中获取p:selectOneMenu的选定值 - Fetch selected value of p:selectOneMenu in JavaScript 如何使用php或javascript单击添加按钮后动态添加选择框字段并将所选值存储在数据库中的所选框中 - How to add select box field dynamically after clicking add button by using php or javascript and store selected value in selected box in database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM