在淘汰赛中无法获得输入字段值

时间:2016-01-15 11:49:03

标签: javascript knockout.js

我正在尝试使用knockout从输入字段传递一个值,所以这是一个表单

<form>
  <input id="amount"  type="text"  placeholder="Amount in &euro;" data-bind="value : amount" />
  <button class="button1" type="submit"  data-bind="click: $root.invest_first_pageview" >Invest</button> 
  <button class="button2" type="submit"  data-bind="click: $root.borrow_first_pageview" >Borrow</button> 
</form>

我试图在我的淘汰赛js文件中获得这样的值,

self.invest_first_pageview = function () {
    alert("HELLO");
    window.location.href = BASEURL + "index.php/moneyexchange/invest_first_page/" + self.amount();
};

// Navigates to the desired URL allowing editing the money specified.
self.borrow_first_pageview = function () {
    window.location.href = BASEURL + "index.php/moneyexchange/borrow_first_page/" + self.amount();
};

但看起来self.amount()没有得到任何值。

1 个答案:

答案 0 :(得分:1)

如果您的amount观察点属于同一范围,例如Fiddle,则它应如https://github.com/Azure-Samples/active-directory-dotnet-graphapi-console所示。没有foreach次迭代包含您显示的代码段。您可以将data-bind="value : amount"更改为data-bind="value : $root.amount"

你的模特是否也近在咫尺:

var viewModel = function() {
    var self = this;
    self.amount = ko.observable();
    self.invest_first_pageview = function() {
        alert(self.amount());
        window.location.href = BASEURL + "index.php/moneyexchange/invest_first_page/" + self.amount();
    };

    // Navigates to the desired URL allowing editing the money specified.
    self.borrow_first_pageview = function() {
        window.location.href = BASEURL + "index.php/moneyexchange/borrow_first_page/" + self.amount();
    };
};