KnockoutJs 可观察数组和下拉列表

发布于 2025-01-03 05:12:36 字数 1136 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-01-10 05:12:36

您可以将下拉列表的值绑定到 selectedPlatform,如下所示:

<select data-bind="options: platforms, value: selectedPlatform, optionsText: 'name'"></select>

我修改了您的代码并对您想要执行的操作进行了一些最佳猜测并创建了一个示例。
这是小提琴: http://jsfiddle.net/johnpapa/DVXH7/

You can bind the dropdown list's value to the selectedPlatform, like this:

<select data-bind="options: platforms, value: selectedPlatform, optionsText: 'name'"></select>

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文