次のノックアウトjsバインディングがあるとします
var optionsWithCategory = [
"Category: Person", // Category option should be disabled, or non-selectable
"Jason",
"John",
"Category: Department",
"Human Resource",
"Sales"
];
<div id="selectBox">
<select databind="options: optionsWithCategory"></select>
</div>
で始まるテキスト値を持つdisable="disable"
すべてのDOM 要素に追加するために使用できるアプローチのいくつか そのため、実際には選択できませんが、選択ボックスの区切りとして機能します。また、ボックスはdiv 要素に動的に追加されます。options
Category:
Person
Department
select
selectBox
私が思いついた1つの方法は次のとおりです。
$('#selectBox').bind('DOMNodeInserted', function (e) {
if (e.target.text != undefined && e.target.text.indexOf(':') >= 0) {
$(e.target).attr("disabled", "disabled");
}
});
しかし、それはあまりにもエレガントに思えません. 他の方法はありますか?