我正在尝试使用 knockoutjs 数据绑定动态设置选择输入的值。如果我只是value: $parents[0].selectedSubcategory
输入<select>
元素的属性,它就可以正常工作。但是当我尝试传递值时,它不起作用。
我的观点是这样的——
<div data-bind="with: QuestionFilter">
<form>
<div data-bind="foreach: details">
<select data-bind="options: subcategories, optionsText: 'name', optionsValue: 'categoryID', value: subcategoriesValue">
</select>
</div>
</form>
</div>
<script type="text/javascript">
ko.applyBindings({
categories = <?php echo $categories; ?>,
details = ko.observableArray([])
});
</script>
我的 JS 看起来像这样 -
function QuestionFilter(categories, details) {
var self = this;
self.subcategories = ko.observableArray([]);
self.selectedSubcategory = ko.observable();
function search(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].parentCategory_id === nameKey) {
self.subcategories.push(myArray[i]);
}
}
}
search(2, categories);
details.push({ firstName: self.subcategories(), lastName: self.selectedSubcategory()});
});
self.selectedSubcategory.subscribe(function(subcategory) {
function subsearch(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].parentCategory_id === nameKey) {
self.superSubcategories.push(myArray[i]);
}
}
}
subsearch(subcategory, categories);
details.push({ firstName: self.superSubcategories()});
});
除了上述之外,我还尝试使用lastName: $parents[0].self.selectedSubcategory()
, 带和不带引号。我也尝试过<select>
像这样设置元素的值:value: $parents[0].lastName
也有和没有引号 and +
,但没有乐趣。
需要任何想法或澄清吗?