3

我刚刚创建了一个 jsfiddle,以便您可以快速编辑 JSFIDDLE Link to Code

如您所见,Array selectedChoice在下拉列表中工作正常,并根据下拉选择的值显示内容。

但是,当我尝试使用CountryModel带有属性 id 和名称的 a 时,它会在 Internet Explorer 中引发错误,但在 Firefox 中则不会。但是,奇怪的是默认行为不是隐藏内容 <p>This content appear when US is selected</p>

我怀疑这段代码

    <section data-bind="visible: selectedChoiceWithModel().name==='US'"> 

我也试过这个

<section data-bind="if: selectedChoiceWithModel().name === 'Russia', 
     hasfocus: selectedChoiceWithModel().name === 'Russia'">
    <p>This content appear when Russia is selected</p>

两个问题:

在此处输入图像描述

打字稿代码

class CountryModel {
    id: number;
    name: string;

}

编译为

var CountryModel = (function () {
    function CountryModel() {
    }
    return CountryModel;
})();

打字稿代码 /// /// ///

class ViewModel {
    constructor()
    {
        //initialize the data for the model now this has two purposes. Consider separating the model from its data generation. 
        var x = new CountryModel();
        x.id = 1;
        x.name = "Russia";
        var y = new CountryModel();
        y.id = 2;
        y.name = "US";
        this.countries.push(x);
        this.countries.push(y);
    }

    availableDrugs = ['A', 'B', 'others'];
    firstName: KnockoutObservable<string>  = ko.observable();
    isVisible: KnockoutObservable<boolean> = ko.observable(true); 
    selectedChoice = ko.observable();
    selectedChoiceWithModel = ko.observable();
    countries: KnockoutObservableArray<CountryModel> = ko.observableArray([]);
    sendMe = function () {

        alert(ko.toJSON({ selectedCountryId: this.selectedChoice() }));
    };
}


$(() => {
    ko.applyBindings(new ViewModel(), document.getElementById("model"));
});

编译为

/// <reference path="CountryModel.ts" />
/// <reference path="../Scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../Scripts/typings/jquery/jquery.d.ts" />
var ViewModel = (function () {
    function ViewModel() {
        this.availableDrugs = ['A', 'B', 'others'];
        this.firstName = ko.observable();
        this.isVisible = ko.observable(true);
        this.selectedChoice = ko.observable();
        this.selectedChoiceWithModel = ko.observable();
        this.countries = ko.observableArray([]);
        this.sendMe = function () {
            alert(ko.toJSON({ selectedCountryId: this.selectedChoice() }));
        };
        //initialize the data for the model now this has two purposes. Consider separating the model from its data generation.
        var x = new CountryModel();
        x.id = 1;
        x.name = "Russia";
        var y = new CountryModel();
        y.id = 2;
        y.name = "US";
        this.countries.push(x);
        this.countries.push(y);
    }
    return ViewModel;
})();

$(function () {
    ko.applyBindings(new ViewModel(), document.getElementById("model"));
});

html代码

<!--http://jsfiddle.net/pkysylevych/dqUAz/2/
    http://stackoverflow.com/questions/12516123/use-knockout-to-hide-display-questions-based-on-selected-value-in-drop-down
    http://jsbin.com/egacil/2/edit
    http://www.codeproject.com/Articles/342244/Knockout-that-cascading-dropdown

    -->
@section scripts
{
    <script src="~/js/RequestFormModel.js"></script>
    <script src="~/js/CountryModel.js"></script>
}
<div id="model">

<p>First name: <strong data-bind="text: firstName"></strong></p>


<p>First name: <input data-bind="value: firstName" /></p>

   <input type="checkbox" data-bind="checked: isVisible"/>
   <div data-bind="if: isVisible">Hide this content.</div>

        <!--Display content usign observable array--> 
    <select data-bind="options: availableDrugs, value: selectedChoice, optionsCaption: 'choose..'"></select> 
      <input type="text" data-bind="value: firstName, visible: selectedChoice() === 'others', hasfocus: selectedChoice() === 'others'" /> 


    <section data-bind="visible: selectedChoice() === 'A', hasfocus: selectedChoice() === 'A'">
        <p> This content appear when a is selected</p>

    </section>
    <section data-bind="visible: selectedChoice() === 'B', hasfocus: selectedChoice() === 'B'">
        <p>This content appear when B is selected</p>
    </section>




    <!---Sample number two with models instead of just an array --> 
      <select data-bind="options: countries, optionsText: 'name', value: selectedChoiceWithModel, optionsCaption: 'Choose...'"></select>



            <section data-bind="visible: selectedChoiceWithModel().name==='US'">
            <p>This content appear when US is selected</p>
    </section>


</div>
4

2 回答 2

1

我怀疑您的绑定在 ie 中中断,而不是在其他更好的浏览器中,因为 ie 无法处理大小写错误。

hasFocus 绑定是 camelCase,因此请尝试将所有 hasfocus 绑定转换为 hasFocus。

编辑

好吧,你的问题不止一个——

http://jsfiddle.net/BJhQz/14/

在尝试绑定到其上的属性之前,您需要确保已定义 selectedChoiceWithModel。我向您的视图添加了一个无容器绑定,以防止在尚未选择它的情况下尝试查看它的名称。

接下来,我对您的视图模型进行了一些更新,以使其更易于理解。我并不是说它们是必需的,但是没有它们,您的视图模型会因我无法阅读而受到影响。

StackOverflow.com 需要一些代码,所以这里是 -

<!-- ko if: selectedChoiceWithModel() -->
    <section data-bind="visible: $data.selectedChoiceWithModel().name() === 'Russia'">
        <p>This content appear when Russia is selected</p>
    </section>
    <h1 data-bind="text: selectedChoiceWithModel().name()"></h1>
<!-- /ko -->
于 2013-08-18T04:09:02.790 回答
0

首先,我真的很感谢大家的帮助。您确实帮助我确定了代码的一些问题。我已经完成了代码,这是未来可能会像我一样痛苦的人的结果。我确实想指出 selectedModelWirhChoice().name() 是错误的方法。相反,关键是放置 optionsValue: 'name' 以便 selectedChoiceWithModel() 绑定到属性名称。当然,也可以选择执行以下 optionsValue: 'id' 来绑定到 id 号。

JsFiddle 代码为您提供方便

为了您的方便,我已经包含了完整的 JSFiddle,但不幸的是,我将显示已编译的 JavaScript 而不是干净的 Typescript 代码。无论如何,打字稿代码如下......

<html>
<head>
    <script src="~/js/RequestFormModel.js"></script>
    <script src="~/js/CountryModel.js"></script>
</head>
<body>
<div id="model">

<p>First name: <strong data-bind="text: firstName"></strong></p>


<p>First name: <input data-bind="value: firstName" /></p>

   <input type="checkbox" data-bind="checked: isVisible"/>
   <div data-bind="if: isVisible">Hide this content.</div>

        <!--Display content usign observable array--> 
    <select data-bind="options: availableDrugs, value: selectedChoice, optionsCaption: 'choose..'"></select> 
      <input type="text" data-bind="value: firstName, visible: selectedChoice() === 'others', hasFocus: selectedChoice() === 'others'" /> 


    <section data-bind="visible: selectedChoice() === 'A', hasFocus: selectedChoice() === 'A'">
        <p> This content appear when a is selected</p>

    </section>
    <section data-bind="visible: selectedChoice() === 'B', hasFocus: selectedChoice() === 'B'">
        <p>This content appear when B is selected</p>
    </section>




    <!---Sample number two with models instead of just an array --> 
      <select data-bind="options: countries, optionsText: 'name', optionsValue: 'name', value: selectedChoiceWithModel, optionsCaption: 'Choose...'"></select>



        <span data-bind="text: selectedChoiceWithModel() ? selectedChoiceWithModel() : 'This content is displayed when the value is unknown'"></span>.


     <span data-bind="if:selectedChoiceWithModel()==='Russia'">Content is displayed when Russia is selected</span>
         <span data-bind="if: selectedChoiceWithModel() === 'US'">Content is displayed when US is selected</span>
</div>




 <h1>Keep in mind i used typescript to generate the javascript</h1>

</body>
</html>
class CountryModel {

    public id: number;
    public name: string;

}


/// <reference path="CountryModel.ts" />
/// <reference path="../Scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../Scripts/typings/jquery/jquery.d.ts" />

class ViewModel {
    constructor()
    {
        //initialize the data for the model now this has two purposes. Consider separating the model from its data generation. 
        var x = new CountryModel();
        x.id = 1;
        x.name = "Russia";
        var y = new CountryModel();
        y.id = 2;
        y.name = "US";
        this.countries.push(x);
        this.countries.push(y);

    }

    availableDrugs = ['A', 'B', 'others'];
    firstName: KnockoutObservable<string>  = ko.observable();
    isVisible: KnockoutObservable<boolean> = ko.observable(true); 
    selectedChoice = ko.observable();
    selectedChoiceWithModel = ko.observable();
    countries: KnockoutObservableArray<CountryModel> = ko.observableArray([]);
    sendMe = function () {

        alert(ko.toJSON({ selectedCountryId: this.selectedChoice() }));
    };
}


$(() => {
    ko.applyBindings(new ViewModel(), document.getElementById("model"));
});
于 2013-08-18T05:30:32.333 回答