Javascript 使用jQuery.val()更新Ember TextField的值

Javascript 使用jQuery.val()更新Ember TextField的值,javascript,jquery,ember.js,Javascript,Jquery,Ember.js,我的观点是: //this code is within a {{#each item in controller.content}} so id will not be unique //so by giving it just an id like i have is not going to work {{#each item in controller.content}} <div class="pull-right" id="qnty-bulk">{{view

我的观点是:

 //this code is within a {{#each item in controller.content}} so id will not be unique
 //so by giving it just an id like i have is not going to work 
 {{#each item in controller.content}}
   <div class="pull-right" id="qnty-bulk">{{view Ember.TextField class="span1 qnty-bulk" id="qnty-bulk" valueBinding="item.qnty" type="number" min="1" max="9999999" disabled=true}}</div>
   <button class="pull-right" {{ action "increase" }}>
       Up
   </button>
 {{/each}}
每次单击“向上”按钮时,我想将文本字段的值增加1 我该怎么做?
我可以使用jquery吗?

您可以使用jquery。但我认为您缺少数据绑定的概念

您使用
item.qnty
属性为
TextField
进行了值绑定

您的递增函数如下所示:

actions: {
    increase: function() {
        var quantity = this.get('model.item.qnty');
        this.set('model.item.qnty', quantity++);
    },
}
您甚至可以使用快捷方式功能:

actions: {
    increase: function() {
        this.increaseProperty('model.item.qnty');
    },
}
Ember将自动检测到
项.qnty
已更改,并更新文本字段中的值

除了使用Ember框架,您永远不应该使用任何其他方法更新您的Ember值。这样做可能会导致您的余烬应用程序中断,或者在本例中无法按预期工作

根据您的评论进行编辑。

你目前的哈佛商学院:

{{#each item in controller}}
    <div {{action increase}} ></div>
{{/each}}
您的MyItemController:

App.MyItemController = Ember.ObjectController.extend({

    actions: {
        increase: function(){
            this.increaseProperty('model.qnty');
        }
    }
})
actions: {
increase: function(item) {
    var qnty = Ember.get(item,'qnty');
    Ember.set(item,'qnty',++qnty);
 }
}

这将触发项目控制器中的
增加
功能,您可以直接访问项目。数组中有一个ArrayController,数组中的项目有一个ObjectController总是好的。

您可以使用jQuery。但我认为您缺少数据绑定的概念

您使用
item.qnty
属性为
TextField
进行了值绑定

您的递增函数如下所示:

actions: {
    increase: function() {
        var quantity = this.get('model.item.qnty');
        this.set('model.item.qnty', quantity++);
    },
}
您甚至可以使用快捷方式功能:

actions: {
    increase: function() {
        this.increaseProperty('model.item.qnty');
    },
}
Ember将自动检测到
项.qnty
已更改,并更新文本字段中的值

除了使用Ember框架,您永远不应该使用任何其他方法更新您的Ember值。这样做可能会导致您的余烬应用程序中断,或者在本例中无法按预期工作

根据您的评论进行编辑。

你目前的哈佛商学院:

{{#each item in controller}}
    <div {{action increase}} ></div>
{{/each}}
您的MyItemController:

App.MyItemController = Ember.ObjectController.extend({

    actions: {
        increase: function(){
            this.increaseProperty('model.qnty');
        }
    }
})
actions: {
increase: function(item) {
    var qnty = Ember.get(item,'qnty');
    Ember.set(item,'qnty',++qnty);
 }
}

这将触发项目控制器中的
增加
功能,您可以直接访问项目。数组中最好有一个ArrayController,数组中的项目最好有一个ObjectController。

不应该使用jQuery

您可以将内容中的单个项目传递给
增加
操作,并在操作中增加其值

<div class="pull-right">
 {{input value=item.qnty type="number" min="1" max="9999999" disabled=true}}
</div>
<button class="pull-right" {{ action "increase" this}}>
 Up
</button>

一个示例,用于您的用例。

您不应该使用jQuery

您可以将内容中的单个项目传递给
增加
操作,并在操作中增加其值

<div class="pull-right">
 {{input value=item.qnty type="number" min="1" max="9999999" disabled=true}}
</div>
<button class="pull-right" {{ action "increase" this}}>
 Up
</button>

一个示例,用于您的用例。

谢谢,但文本字段中没有显示任何内容,我收到一个错误:未捕获错误:路径模型中的对象。无法找到或已销毁项目?我想知道你的
商品.qnty
。什么是
?。您可以在没有模型前缀的情况下尝试上面的代码。项目是:{{{#controller.content}我是新加入ember的,正在尝试动态学习它。我已经完成了ToDo教程,但还没有完全理解,这项工作必须完成!:)谢谢,但是文本字段中没有显示任何内容,我得到一个错误:未捕获错误:路径模型中的对象。无法找到项或已销毁?我想知道你的
商品.qnty
。什么是
?。您可以在没有模型前缀的情况下尝试上面的代码。项目是:{{{#controller.content}我是新加入ember的,正在尝试动态学习它。我已经完成了ToDo教程,但还没有完全理解,这项工作必须完成!:)您将在阵列控制器中放置项逻辑。在我看来,这是一个糟糕的做法。您将在阵列控制器中放置项逻辑。在我看来,这是一种不好的做法。