Javascript 无法在敲除中获取输入字段值

Javascript 无法在敲除中获取输入字段值,javascript,knockout.js,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" >In

我试图使用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>

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

如果您的
金额
可观察值在同一范围内,则它应该如中所示工作,这意味着没有包含您显示的片段的
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();
    };
};

请编辑您的问题,包括完整(但最少)的修改,并解释您所说的“没有得到任何值”是什么意思。@Jeroen我的意思是,当我在提交时调用函数时,我在self中没有得到任何值。amount()对我来说仍然不清楚。“没有任何价值”是什么意思?它是否等于
null
?或
未定义
?还是一个空字符串?还是零?或者它会抛出一个错误。。。在任何情况下,您当前发布的代码都不会复制这些内容。@Jeroen嗯,我忘了为金额输入一个ko observable,所以当我单击按钮时,我在该observable中没有得到任何值,因此在我的警报语句中它是空的,如果不清楚,很抱歉,但是我从下面得到了我的答案。如果问题没有包含足够的信息,请不要猜测OP的问题。相反,请OP在评论中澄清,并在适当的情况下标记为关闭主题。@Jeroen嘿,这不是关闭topic@NikolayErmakov,我忘了把self.amount=ko.observable()放进去;那是我的错误,谢谢!!!
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();
    };
};