如何使 dijit.form.Select 与具有相同值的多个项目一起使用?
Posted
技术标签:
【中文标题】如何使 dijit.form.Select 与具有相同值的多个项目一起使用?【英文标题】:How can I make a dijit.form.Select work with multiple items having the same value? 【发布时间】:2012-05-22 16:49:28 【问题描述】:我有一个dijit.form.Select
小部件,用于将标签映射到值。一些标签需要具有相同的值,但我需要能够在进行选择时区分标签。现在小部件的选项看起来像这样:
[
label: "A",
value: "1",
,
label: "B",
value: "2"
,
label: "C",
value: "2"
],
That is done because the form's processing needs to know that 'B' and 'C' both actually mean 2, but I need to perform some different logic on another control when 'B' vs 'C' is selected.我发现我可以在onChange
事件处理程序中使用get("displayedValue")
来获取“显示”标签,但它总是会返回与当前值匹配的 first 选项(B)(2 ) 这不一定是用户选择的内容。
那么,当多个标签需要评估为相同的值同时仍然能够区分标签时,我该如何处理?
【问题讨论】:
【参考方案1】:只需使用 dijit/form/Select 小部件focusNode.textcontent
属性:
function disaplySelected()
document.getElementById("labelContainer").innerhtml = window.sel.focusNode.textContent;
document.getElementById("valueContainer").innerHTML = window.sel.value;
require(["dijit/form/Select", "dojo/_base/window", "dojo/domReady!"], function(Select, win)
window.sel = new Select(
name: "select2",
options: [
label: "A",value: "1",
label: "B",value: "2",
label: "C",value: "2"
],
);
window.sel.placeAt(win.body(), "first").startup();
);
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.1/dojo/dojo.js" data-dojo-config="async: true"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.1/dijit/themes/claro/claro.css">
</head>
<body class="claro">
<p>
<button onclick="disaplySelected()">Display selected label</button>
<br/>Selected item label: <span id="labelContainer"></span>
<br/>Selected item value: <span id="valueContainer"></span>
</p>
</body>
</html>
【讨论】:
以上是关于如何使 dijit.form.Select 与具有相同值的多个项目一起使用?的主要内容,如果未能解决你的问题,请参考以下文章
如何添加 optgroup do dijit.form.Select 或其他小部件类型
用dstore / Memory存储器存储来填充dijit / form / select