【问题标题】:KnockoutJs: Getting value from dropdownlistKnockoutJs:从下拉列表中获取价值
【发布时间】:2013-06-14 03:00:27
【问题描述】:

我很难使用 Knockout JS 获取下拉列表的选定值

jsFiddle

HTML

     <select id="l" data-bind="options: locations, value=selectedLocation"></select>
     <select id="j" data-bind="options: jobTypes, value=selectedJobType"></select>
     <button data-bind="click: myFunction"> Display </button>

脚本

       var viewModel = {
            locations: ko.observableArray(['All Locations', 'Sydney', 'Melbourne', 'Brisbane', 'Darwin', 'Perth', 'Adelaide']),
            selectedLocation: ko.observable(),

            jobTypes: ko.observableArray(['All Vacancies', 'Administration', 'Engineering', 'Legal', 'Sales', 'Accounting']),
            selectedJobType: ko.observable(),

            myFunction: function() {
                alert(selectedJobType + ' ' +selectedLocation );
            }
        };
        // ... then later ...
        //viewModel.availableCountries.push('China');
        // Adds another option
        ko.applyBindings(viewModel);

【问题讨论】:

    标签: javascript jquery html knockout.js knockout-2.0


    【解决方案1】:

    应该是这样的

    value:selectedLocation
    

    和:

    value:selectedJobType
    

    在你的绑定中。绑定使用与对象字面量相同的语法。

    此外,在您的警报中,您需要 viewModel.selectedJobType(),因为 (a) 它是 viewModel 的属性,而不是全局属性,并且 (b) 它是可观察的,因此您需要调用它来获取值。 selectedLocation 也一样。

    这是一个有效的fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多