KnockoutJs 可观察数组和下拉列表
我是 KnockoutJs 的新手,我想知道是否有人可以提供帮助。
我有一个视图模型,由 Mvc3 控制器填充,绑定到下拉列表,并且工作正常。
我在“平台”observableArray 中存储了额外的数据,并且 我希望这些数据显示在文本框中,具体取决于 在下拉列表中选择的值。
到目前为止,这是我的代码:-
<script type="text/javascript">
$(document).ready(function () {
var sampleSubmission = function () {
this.selectedPlatform = ko.observable();
this.platforms = ko.observableArray();
this.showSearch = ko.observable(false);
this.craftText = ko.observable();
this.showSerialNumber = ko.observable(0);
this.selectedPlatform.subscribe(function (platformId) {
} .bind(this));
};
var sampleSubmissionViewModel = new sampleSubmission();
ko.applyBindings(sampleSubmissionViewModel);
//Load the platforms
$.ajax({
url: '@Url.Action("GetPlatforms", "Home")',
type: 'GET',
success: function (data) {
sampleSubmissionViewModel.platforms(data);
}
});
});
</script>
有人知道我如何实现这一目标吗?
提前致谢。
I'm new to KnockoutJs and I wonder if anyone can help with this.
I have a viewmodel, populated from a Mvc3 controller, bound to a dropdown and this is working fine.
I have additional data stored in the "platforms" observableArray, and
I would like this data to be displayed in textboxes, dependant on the
selected value in the dropdown.
Here is my code so far:-
<script type="text/javascript">
$(document).ready(function () {
var sampleSubmission = function () {
this.selectedPlatform = ko.observable();
this.platforms = ko.observableArray();
this.showSearch = ko.observable(false);
this.craftText = ko.observable();
this.showSerialNumber = ko.observable(0);
this.selectedPlatform.subscribe(function (platformId) {
} .bind(this));
};
var sampleSubmissionViewModel = new sampleSubmission();
ko.applyBindings(sampleSubmissionViewModel);
//Load the platforms
$.ajax({
url: '@Url.Action("GetPlatforms", "Home")',
type: 'GET',
success: function (data) {
sampleSubmissionViewModel.platforms(data);
}
});
});
</script>
Does anyone have any ideas how I achieve this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将下拉列表的值绑定到 selectedPlatform,如下所示:
我修改了您的代码并对您想要执行的操作进行了一些最佳猜测并创建了一个示例。
这是小提琴: http://jsfiddle.net/johnpapa/DVXH7/
You can bind the dropdown list's value to the selectedPlatform, like this:
I modified your code and took some best guesses at what you wanted to do and created a sample.
Here is the fiddle: http://jsfiddle.net/johnpapa/DVXH7/