如何在Knockout中声明变量内联

时间:2015-03-31 18:35:13

标签: javascript html asp.net-mvc knockout.js

如何在淘汰赛中声明变量内联?我声明someValue的方式给了我错误。

<input data-bind="textInput : new someValue ,event: {keypress: enterSearch}" class="form-control" placeholder="Jump to ...">

<button type="button" class="btn btn-default" data-bind="click: $root.selectPage.bind($data, someValue)"> Search! </button>

1 个答案:

答案 0 :(得分:0)

HTML:

<input data-bind="value: page" class="form-control" placeholder="Jump to ...">

<button type="button" class="btn btn-default" data-bind="click: selectPage"> Search! </button>

viewmodel(约):

var viewmodel = function() {
  var page = ko.observable();

  var selectPage = function() {
    // just use page() here instead of passing it as a parameter

  };

  return {
    page: page,
    selectPage: selectPage
  }
}