javascript - 检查在 jQuery Bootstrap 多选中单击了哪个 optgroup
问题描述
我想知道回调函数中提供的option
参数是否代表元素。onChange
optgroup MyGroup
当我在 Chrome 调试器中探索option
时,我得到以下我不理解的输出:
我在互联网上找到的有关信息n.fn.init
告诉我该对象尚未实例化,因此它以这种方式显示。我在 Javascript/jQuery 方面的知识太差,无法理解并自行解决此问题。
我的问题:
我如何在onChange
函数中访问option
参数?
例如,当我将 an 分配id
给 the时optgroup
,它会帮助我,它会id
被返回,我可以轻松地检查它。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap onChange Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/davidstutz/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/davidstutz/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css"/>
<script type="text/javascript">
$(document).ready(function() {
$('#example').multiselect(
{
enableClickableOptGroups: true,
onChange: function(option, select)
{
// how to access option.???
}
}
)
});
</script>
</head>
<body>
<select id="example" multiple="multiple">
<optgroup label="MyGroup" id="MyGroupId">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</optgroup>
</select>
</body>
</html>
解决方案
option
在您的函数参数中可能是一个 jQuery 对象数组。
因为option
在这个例子中可以是一个选项数组,由于选择了一个optgroup
你需要迭代或映射数组并提取你需要的信息而改变的选项。
在这个例子中,一个选项值数组被输出到控制台。
根据要求编辑此示例现在还输出一个选项数组id
。
艾尔邦迪编辑:
将以下代码放入onChange
回调函数适用于所有可能性。optgroup
当只有一个元素时,Steve0 的第一个版本不起作用。此外,我的扩展代码 - 基于原始 Steve0 的答案 - 返回id
foroptgroup
或item
取决于哪个被点击:
optionId = Array.isArray(option)
? $(option[0]).closest("optgroup").attr("id")
: option.attr("id");
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap onChange Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/davidstutz/bootstrap-multiselect/dist/js/bootstrap-multiselect.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/davidstutz/bootstrap-multiselect/dist/css/bootstrap-multiselect.css" type="text/css"/>
<script type="text/javascript">
$(document).ready(function() {
$('#example').multiselect(
{
enableClickableOptGroups: true,
onChange:function(option, checked, value)
{
//console.log(option);
console.log($.map(option, x=>{return $(x).val();}));
//console.log($.map(option, x=>{return $(x).attr("id");}));
if(option.length > 1){
//if I have more than one picked, then return the ID of the first-items group
console.log($(option[0]).closest("optgroup").attr("id"));
}
// how to access option.???
//console.log($(option).val());
// Al Bundy extension:
optionId = Array.isArray(option)
? $(option[0]).closest("optgroup").attr("id")
: option.attr("id");
}
}
)
});
</script>
</head>
<body>
<select id="example" multiple="multiple">
<optgroup label="MyGroup" id='YourGroup'>
<option id="opt1" value="1">Option 1</option>
<option id="opt2" value="2">Option 2</option>
<option id="opt3" value="3">Option 3</option>
</optgroup>
</select>
</body>
</html>
推荐阅读
- python-3.x - 从文件名中提取字符并将其作为值添加到 csv python
- spring - 为什么 spring.jpa.hibernate.ddl-auto 设置更新但 schema.sql 和 data.sql 仍在执行?
- html - 连接
- 和
- 在
我想创建页眉,我需要在一行中连接 dt 和 dd 这是我的视图代码:
<div style="margin-left:100px"> <hr /> <dl class="horizontal"> <dt>@Html.DisplayNameFor(model => model.labCashView.Patient_Nam
- php - Laravel:将“selected”属性添加到 HTML 选择选项,无论是从旧值还是数据库
- c - 使用 strcpy 和 strncpy 将目录中的文件列出到 C 中的数组中
- python - pytest html - 从测试文件将图像传递给 conftest.py 中的钩子
- google-bigquery - 比较表中的两列,得到相似和不相似的值
- python - conda env export --from-history 不跟踪频道
- dataframe - 如何在pyspark中按有序分类变量创建和排序
- shell - 如何为 gsub 使用动态字段值?