-
2021-06-11 02:20:46
本文将介绍怎样使用JQuery动态增加下拉列表选项(option)。
项目(选项)文本和值部分将从文本框中动态提取,然后项目(选项)将使用jquery添加到HTML下拉列表中。
使用JQuery动态增加下拉列表选项
使用JQuery动态增加下拉列表选项
下面的HTML标记由一个HTML下拉列表组成,在使用jquery单击按钮时,项目(选项)将被动态添加到该列表中。
它还包含两个HTML文本框和一个HTML按钮元素。这两个HTML文本框将用于捕获HTML下拉列表的文本和值部分,而HTML按钮将用于向HTML下拉列表添加项(选项/option)。
Text:
Value:
$(function() {
$("#btnAdd").click(function () {
var option = $("");
option.html($("#txtText").val());
option.val($("#txtValue").val());
$("#ddlFruits").append(option);
});
});
解释:
首先,要调用jquery库文件。
其次,程序实现部分,单击“添加(add)”按钮时,将执行jquery click事件处理程序,其中首先引用HTML下拉列表,然后创建HTML选项元素。
文本部分使用html() jquery函数设置,值部分使用html option元素的val() jquery函数设置。
option.html()
option.val()
最后,使用append()jquery函数,HTML选项被添加到下拉列表中。
您可能对以下文章也感兴趣
更多相关内容 -
JavaScript实现下拉列表框数据增加、删除、上下排序的方法
2020-12-10 01:34:29这里实现在一个支持多选的下拉列表框内进行数据项的添加、删除、向上、向下移动操作,我们在一些人才网站或是编程技术站经常会看到这种功能,比较实用。 运行效果截图如下: 具体代码如下: <title>下拉列表... -
Excel 多级下拉列表例子
2015-01-09 10:49:32Excel 多级下拉列表例子 用于在Excel中创建三级下拉列表。 -
excel创建下拉列表多选_如何在Excel中创建下拉列表?
2020-10-13 10:13:20excel创建下拉列表多选Excel provides the drop-down list in order to provide multiple options to select one. The drop-down list can be created in different ways and options. In this tutorial, we will ...excel创建下拉列表多选
Excel provides the drop-down list in order to provide multiple options to select one. The drop-down list can be created in different ways and options. In this tutorial, we will learn how to create a drop-down list for excel.
Excel提供了下拉列表,以便提供多个选项来选择一个。 可以使用不同的方式和选项创建下拉列表。 在本教程中,我们将学习如何为excel创建一个下拉列表。
什么是下拉列表? (What Is Drop-down List?)
The drop-down list provides multiple options where these options are selected from a list that works as a drop-down and select. The selected value or item will be set as the value for the drop-down list.
下拉列表提供了多个选项,可从用作下拉列表的列表中选择这些选项并进行选择。 所选值或项目将被设置为下拉列表的值。
创建下拉列表 (Create Drop Down List)
As the drop-down list consist of multiple options or items of list we need some data source to provides as options in the drop-down list. So we will create a list which is countries in this example which contains,
Turkey, UK, Germany, USA, China
.We will also put a header for this list which isItems
. But using the header is optional.由于下拉列表包含多个选项或列表项,因此我们需要一些数据源作为下拉列表中的选项提供。 因此,在此示例中,我们将创建一个包含国家/地区的列表,其中包含
Turkey, UK, Germany, USA, China
。我们还将在该列表的标头中放置Items
。 但是使用标头是可选的。In order to use this list as the drop-down list source, we should convert them into a table. We can convert the list by selecting them and using
CTRL+T
shortcut. We are free to select the header Items but if we select it for conversion then we should express that the table has a header in the table creation dialog box.为了将此列表用作下拉列表源,我们应该将它们转换为表格。 我们可以通过选择列表并使用
CTRL+T
快捷键来转换列表。 我们可以自由选择标题Items,但是如果选择它进行转换,则应在表创建对话框中表示该表具有标题。Convert Items To Table 将项目转换为表格 We can see the following screen which will convert given range items into a table. We will check the
My table has headers
checkbox if we selected the Item header cell.我们可以看到以下屏幕,它将给定的范围项目转换为表格。 如果选择了项目标题单元格,我们将选中“
My table has headers
复选框。Create Table From the Items 从项目创建表 We can see below that the items are listed inside the table as a table structure where items are colored accordingly. Also the header is colored more dark. We will use the following table in order to create the drop-down list.
我们可以在下面看到,表中列出的项目是作为表结构列出的,其中项目相应地带有颜色。 标头的颜色也更深。 我们将使用下表来创建下拉列表。
Drop-down List Source Table 下拉列表源表 Now we will start the drop-down list creation process. First we will select a cell where the drop-down list will be shown. In this case we select the
D2
cell. Then we will openData Validation
fromData Tools
menu.现在,我们将开始下拉列表创建过程。 首先,我们将选择一个显示下拉列表的单元格。 在这种情况下,我们选择
D2
单元。 然后,我们将从“Data Tools
菜单中打开“Data Validation
。Open Drop-down list Creation Dialog Box 打开下拉列表创建对话框 Be low we can see the
Data Validation
screen where we will make some configuration. We will set theAllow:
settings asList
from theSettings
tab.Ignore Blank
will ignore blank cells of the table which is useful in most of the cases.In-cell drop-down
will create the drop-down action and it must be selected.Source
provides the range of the items we want to put into the drop-down list.较低时,我们可以看到“
Data Validation
屏幕,在此处进行一些配置。 我们将从“Settings
标签中将“Allow:
设置设置为“List
Settings
。Ignore Blank
将忽略表的空白单元格,这在大多数情况下很有用。In-cell drop-down
将创建下拉菜单操作,必须选中它。Source
提供了我们要放入下拉列表的项目范围。Configure Drop-down List Options 配置下拉列表选项 We can also provides some help information or message about the cell for better user experience. In the
Input Message
tab we can set some message about the drop down list. We will check theShow input message when cell is selected
checkbox and put theTitle
andInput message
values like below.我们还可以提供一些有关该单元的帮助信息或消息,以提供更好的用户体验。 在
Input Message
选项卡中,我们可以设置一些有关下拉列表的消息。 我们将选中“Show input message when cell is selected
复选框,然后Input message
“Title
和“Input message
值,如下所示。Drop-down List Configuration 下拉列表配置 When everything is completed we will click OK which will create our drop-down list in the cell D2. We will see the information title and message when we select the given drop-down list cell which is D2. We will also see a square button that will open the drop-down list and items to select.
一切完成后,我们将单击OK,这将在单元格D2中创建下拉列表。 当选择给定的下拉列表单元格D2时,我们将看到信息标题和消息。 我们还将看到一个方形按钮,它将打开下拉列表和要选择的项目。
Drop-down List with Title and Message 带有标题和消息的下拉列表 We will see the following drop-down menu when we click it. All items are list which are
Turkey, UK, Germany, USA, China
单击它后,我们将看到以下下拉菜单。 所有项目均列出了
Turkey, UK, Germany, USA, China
Drop-down List Show 下拉列表显示 When we select an option which is Germany, in this case, the value will be shown in the drop-down list cell. This can be very useful to select and read values for different multi-choice applications.
在这种情况下,当我们选择一个选项(德国)时,该值将显示在下拉列表单元格中。 这对于选择和读取不同的多项选择应用程序的值非常有用。
Drop-down List Select Menu 下拉列表选择菜单 通过手动输入数据创建下拉列表(Create Drop Down List By Entering Data Manually)
Another way to provides options to the Drop-down list is providing the data manually. Generally the drop-down options are provided as a table from other cells contents of the Excel. Alternatively we can provide the drop-down list items from the Data Validation dialog box. The process is the same with the creating drop-down list which is described previously. When we arrive the Data Validation dialog box in the source tab we will provide the data manually by separating them with comma.
Comma
is the separator to delimit drop-down list items.为下拉列表提供选项的另一种方法是手动提供数据。 通常,从Excel的其他单元格内容中以表格形式提供下拉选项。 或者,我们可以从“数据验证”对话框中提供下拉列表项。 该过程与前面所述的创建下拉列表相同。 当我们到达“源”选项卡中的“数据验证”对话框时,我们将通过用逗号分隔数据来手动提供数据。
Comma
是分隔列表项的分隔符。Drop-down List with Manual Data 带有手动数据的下拉列表 And all other settings and configurations are the same like table data as source.
所有其他设置和配置都一样,例如表数据作为源。
了解更多有关带示例和Range / Xrange函数的Python For Loop教程通过使用Excel公式/函数创建下拉列表(Create Drop Down List By Using Excel Formulas/Functions)
Up to now, we have provides the drop-down list items like a table or manually. There is another alternative way to provide items for the drop-down list. We can provide the drop-down list items dynamically. We can use some functions in order to create drop-down list items.
到目前为止,我们提供了下拉列表项,例如表格或手动。 还有另一种方法可以为下拉列表提供项目。 我们可以动态提供下拉列表项。 我们可以使用一些功能来创建下拉列表项。
允许不存在的条目 (Allow Not Existing Entries)
By default a drop-down list provides the given items and only one of these items can be selected. No other value can be selected or entered into the drop-down list cell by default. If we need to accept optional values which are not presented in a drop-down list we should disable
Show error alert after invalid data is entered
checkbox and feature. This features is located in theError alert
tab of the Data validation dialog box like below. It can be change during a drop-down list creation or after the creation by editing the drop-down list properties.默认情况下,下拉列表提供给定的项目,并且只能选择这些项目之一。 默认情况下,无法选择其他值或将其输入到下拉列表单元格中。 如果我们需要接受下拉列表中未显示的可选值,则应禁用
Show error alert after invalid data is entered
复选框和功能。 此功能位于“数据验证”对话框的“Error alert
选项卡中,如下所示。 可以在创建下拉列表期间或在创建之后通过编辑下拉列表属性来更改它。Allow Not Existing Entries 允许不存在的条目 Below we can see that even
Spain
is not defined as an item of the drop-down list we have typed it into the cell manually.在下面我们可以看到,甚至
Spain
也没有定义为下拉列表中的一项,我们已经手动将其键入到单元格中。Add Manual Data To Drop-Down List 将手动数据添加到下拉列表 添加/删除项目(Add/Remove Items)
New items can be added into the drop-down list easily in different ways. One of the easiest ways is appending the new item into the end of the current source table of the drop-down list. We will add Spain in this example down China which an item of the table. The table will be automatically extended and Spain is added as an item in the drop-down list.
可以通过不同方式轻松地将新项目添加到下拉列表中。 最简单的方法之一是将新项目附加到下拉列表的当前源表的末尾。 在此示例中,我们将在中国下表中添加西班牙。 该表将自动扩展,并且西班牙作为下拉列表中的项添加。
Add/Remove Items 添加/删除项目 删除下拉列表(Remove Drop-down List)
We can remove or delete a drop down list from the Data Validation dialog box. First we will select the cell of the drop-down list. Then we will open the data validation dialog box.
我们可以从“数据验证”对话框中删除或删除下拉列表。 首先,我们将选择下拉列表的单元格。 然后,我们将打开数据验证对话框。
Then in the settings tab we will click to the
Clear All
button which will clear the source. Alternative we can clear the source with the backspace or delete.然后,在设置选项卡中,我们将单击“
Clear All
按钮,这将清除源。 或者,我们可以使用退格键清除源代码或删除源代码。Clear Source Content 清除源内容 After clearing the source content it will look like below as we can see that the Allow setting is any value and data setting is disabled. The last step is clicking
OK
to apply this setting.清除源内容后,如下所示,因为我们可以看到“允许”设置为任何值,并且数据设置被禁用。 最后一步是单击“
OK
以应用此设置。Remove Drop-down List 删除下拉列表 从属/嵌套/多级下拉列表(Dependent/Nested/Multi Level Drop-down List)
Drop-down list can be created in a dependent or nested or multilevel way. All of the same which is simply selected item of the first drop-down list will change the second drop-down item list. This is a hierarchical way. The fist drop-down list is also called parent drop-down list and the second drop-down list is called child drop-down list. Actually dependent drop-down list can be implemented for more than 2 levels like 5 or 10 levels where every child depends its parent selection.
可以以相关或嵌套或多级方式创建下拉列表。 只要是第一个下拉列表的简单选择项,所有这些都将更改第二个下拉列表。 这是一种分层方式。 第一下拉列表也称为父下拉列表,第二个下拉列表称为子下拉列表。 实际上依赖的下拉列表可以实现2个以上的级别,例如5或10个级别,其中每个孩子都取决于其父级选择。
了解更多C-数组翻译自: https://www.poftut.com/how-to-create-a-drop-down-list-in-excel/
excel创建下拉列表多选
-
js下拉列表实现增加和移除选项
2017-08-31 09:46:15select的option属性用法,实现下拉选项的增加和移除,可拓展为下一篇城市级联的基础知识。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> <head> <base href="http://localhost:8080/%E7%BA%A7%E8%81%94/"> <title>JaneYork</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <select id="s1" multiple="multiple"> <option>北京</option> <option>上海</option> </select> <input type="text" id="txt"> <input type="button" value="添加" id="addbtn" οnclick="add()"> <input type="button" value="移除" id="removebtn" οnclick="remove()"> </body>
//主要功能实现 <script> //添加按钮功能实现 function add(){ //获取input文本输入狂标签 var txt = document.getElementById("txt"); //新建一个option var o = new Option(); //将文本框输入的内容赋给option显示的内容 o.text = txt.value; var se = document.getElementById("s1"); //把新建的option添加进来 se.add(o); } //移除按钮功能实现 function remove(){ //获取select标签 var se = document.getElementById("s1"); //移除当前选中项 se.remove(se.selectedIndex); } </script> </html>
页面显示效果:
-
QT学习记录6(QCombobox 下拉列表禁用某个列表项)
2019-03-27 21:54:37项目中经常会遇到这样的情况:由于某些情况的限制,下拉框(QCombobox的一些选项不能被选择,但还不能直接删除不写) ... //第2项 QVariant v(0); //这里的combox是QCombobox控件的名字 ui->comBox-...项目中经常会遇到这样的情况:由于某些情况的限制,下拉框(QCombobox的一些选项不能被选择,但还不能直接删除不写)
如果要实现禁用某些选项,可以这样操作
QModelIndex index = com->model()->index(1, 0); //第2项 QVariant v(0); //这里的combox是QCombobox控件的名字 ui->comBox->model()->setData(index, v, Qt::UserRole - 1);
为了能够禁用多个列表项,可以封装一个函数循环一下
2020年1月10日修改: 增加解除禁用功能 具体内容再代码块的注释部分
void MyClass::set_items_disabled(QComboBox *com, int *a, int len) { for(int i=0; i<len; i++) { QModelIndex index = com->model()->index(a[i], 0); QVariant v(0); //如果要解除,则 QVariant v(-1); com->model()->setData(index, v, Qt::UserRole - 1); qDebug() << "对项目" << a[i] << "进行非使能操作"; } }
这样的话,需要禁用哪些选项,调用函数即可
int a[]={1,2,3,4,6,7,8}; set_items_disabled(com, a, 7);
需要注意的是,这里设置了禁用之后,下拉框被禁用的选项不能选择,点击了,但是颜色和其他项目的颜色是一样的,所以要做一些QSS的操作
使用以下代码即可
ui->mode->setView(new QListView); //设置委托,以应用CSS定制样式
QSS
QComboBox QAbstractItemView::item::!enabled { color:rgb(125,125,125); }
效果是这样的:只有2个列表项是启用的,其余都被禁用了
这个灰色的下拉框QSS,在上一篇博客中有写到
文中有错误或者意见请帮忙指出,多谢!
-
Axure选择下拉列表选项后添加到文本框效果的实现
2020-12-31 07:07:22做原型图需要一个效果展示给前端,即一个输入框支持通过下拉菜单添加多个选项,如下图:点击添加按钮,展示下拉列表框,选择一个选项后,添加到输入框,支持多次添加。对于Axure,都是现学现用,自己研究了下,找到... -
Java图形界面编程--ItemListener 下拉列表框事件监听
2021-03-12 08:13:45ItemListener 下拉列表框事件监听 package com.lddx.day0307; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.... -
【转】使用JS,动态给下拉列表添加option 选项
2020-09-11 10:21:44动态给下拉列表添加option1. 动态创建select2. 添加选项option3. 删除所有选项option4. 删除一个选项option5. 获得选项option的值6. 获得选项option的文本7. 修改选项option8. 删除select 1. 动态创建select ... -
JavaScript实现下拉列表框添加选项
2019-10-18 12:40:27* @ selectId 下拉列表框的id * @ listItems 要添加到列表框中的列表项数组 */ function addListOption(selectId, listItems) { // 循环遍历数组 for (var item in listItems) { // ... -
c#: DataGridView单元格添加下拉列表(combobox)
2021-06-20 16:50:37在DataGridView单元格中显示下拉列表 方式一 直接选择添加一列类型为DataGridViewComboBoxColumn的列,然后添加数据源即可。但是这种方式的下拉列表看起来并不是十分的美观。 方式二 另用下拉列表覆盖当前单元格。 ... -
Python tkinter自定义多选下拉列表框(带滚动条、全选)
2022-03-05 13:28:05Python tkinter自定义多选下拉列表框(带滚动条、全选) -
Unity 下拉菜单中【下拉列表自动加载】 以及 【读取当前item的值】
2021-12-29 16:49:181、如何用代码初始化下拉菜单的items 2、如何乱序加载items 3、如何读取当前选中的item的值 -
Android:实现下拉列表 Spinner
2019-06-05 08:25:11元素实现一个下拉列表,在其中每个下拉列表项使用<option>表示即可。这是在Web开发中一个必不可少的交互性组件,而在Android中的对应实现就是Spinner。 方法一: 以资源方式,静态展示 Spi... -
easyui combobox 下拉列表添加选择项
2015-10-20 17:25:51我们在使用easyui combobox 创建下拉列表时,需要通过ajax获取从服务端返回的json字符串,并显示在客户端。但除了显示数据库中存在的数据,往往需要为返回的结果增加一个选项,比如“全部”。 对于这个问题尝试... -
axure下拉列表框单选框_如何在Axure中创建下拉菜单和组合框
2020-08-05 13:56:26axure下拉列表框单选框First, let’s clarify what exactly is a dropdown menu, and what is a combo box, aren’t they the same? Well … no, not really, let me explain. 首先,让我们弄清楚什么是下拉菜单,... -
Access-控件解析-组合框-01-下拉列表
2019-04-03 20:35:44微信公众号原文 ...涉及功能为:下拉列表的实现 功能动图 Part 2:实现功能 手动设置组合框下拉列表:长江;黄河;珠江;淮河 通过代码修改组合框下拉列表:1;2;3;4 应用场景,组合框的下拉... -
css实现列表下拉菜单_逐行:点击打开下拉列表和菜单的高级CSS技巧
2020-07-28 17:01:54css实现列表下拉菜单by ... 逐行:点击打开下拉列表和菜单的高级CSS技巧 (Line-by-line: advanced CSS tricks for click-to-open drop-down lists and menus) For as long as I can remember, there were always... -
JavaScript:动态生成下拉列表框
2018-10-21 15:52:00范例:动态生成下拉菜单 TestDemo.htm <!DOCTYPE html> <html> <head> <title>下拉框</title> <meta charset="utf-8"> <script type="text/javascript"> var cit..... -
java web 项目中封装的下拉列表小组件:实现下拉列表使用者前后端0行代码 ...
2019-04-18 11:39:05像easy ui 之类的纯前端组件,也有下拉列表组件,但是使用的时候,每个下拉列表,要配一个URL ,以及设置URL反回来的值和 select 的text ,和value 的对应关系 ,这有2个问题:一使用者必须知道URL ,二,如果页面有... -
datatable.js在表格中添加下拉列表
2019-07-18 15:43:18"fnCreatedRow": function(nRow, aData, iDataIndex) {//下拉列表方法 //选择第几列加入下拉按钮 $('td:eq(7)', nRow).prepend("[8] + "'>账号邮箱</button>"); }, }); $('.table').on('click', ' ... -
Android例子源码IOS风格的下拉刷新与加载更多列表组件.zip
2019-07-10 12:50:47一个顺滑又漂亮的Android下拉刷新与加载更多列表组件。 根据系统自带ListView源码改造而来: 增加下拉刷新及滚动到底部自动加载的功能; 增加越界回弹效果; 增加自定义列表项动画的功能; 与其他下拉刷新列表... -
组态王下拉列表框问题
2020-12-21 01:45:14首先认识一下列表框控件的函数:listLoadList("ControlName","Filename")此函数用于将CSV格式文件“Filename”中的列表项调入指定的列表框控件“ControlName”中,并替换列表框中的原有列表项。列表框中只显示列表项... -
016-删除列表框、下拉菜单的选项
2019-03-14 21:55:16删除列表框,下拉菜单的选项有两种方法: 利用HTMLSelectElement对象的remove()方法删除选项。 直接将指定选项赋值为null即可。 对HTMLSelectElement对象而...如果该索引值比下拉列表中选项的最大索引值还大,或者... -
MFC List Control 控件添加单元格编辑和单元格下拉列表项
2016-06-17 11:42:51原理很简单:就是在点击到列表相应单元格的时候,在相应的位置生成一个跟单元格大小相当的编辑框或者下拉列表框并贴在单元格上面而已。 实现如下: 在对话框类声明中添加如下变量和函数:头文件中添加:int e_Item; ... -
axure制作下拉列表模糊查询
2020-06-30 11:14:392、搜索框内的文字长度大于等于1,并且搜索的文字能够模糊匹配下拉列表中的值,显示下拉列表; 3、鼠标悬停在下拉框文字上,改变文字的背景颜色; 4、单击选中下拉列表文字将文字赋值到搜索框,同时隐藏下拉列表; 5... -
关于js动态向下拉列表中添加数据
2015-09-25 14:00:22这个是我在后台传过来的一个json字符串,这里也有点学问,就是把json字符串要转成json对象才能使用的 至于为什么是两层的括号,外层就不说了,因为是evel函数的,里面的自己看别人的记录就是为了"隔离的",因为... -
DOM-动态创建表格和动态添加下拉列表框实例
2014-11-29 11:56:02下面是动态添加下拉列表框: .html代码如下: 动态下拉列表添加 function inertCity(){ var obj = document.getElementById("city"); var option = document.createElement("option");//创建... -
element-ui中的select下拉列表 设置默认值
2021-01-25 18:51:09在element-ui的运用中,涉及到了select下拉列表。项目中需要将select的默认值给展示出来,需要设置默认值 在官方给出的文档时候 <script> export default { data() { return { options: [{ value: '选项1... -
flutter实现一个下拉菜单【基于PopupMenuButton】
2022-03-30 15:35:23提示 已将代码上传至gitee,后续会继续更新学习封装的一些组件: flutter练习 实现效果 !...实现过程 1.封装菜单子项MenuItem .../// 通用菜单项model class MenuItem { String label; // 显示的文本 dynam