我尝试使用knockoutjs data-bind动态设置选择输入的值。如果我只是将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
也有引号和+
,但没有快乐。
需要任何想法或澄清吗?