首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用jQuery.val()更新Ember TextField的值

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

Stack Overflow用户
提问于 2013-11-11 16:12:45
回答 2查看 344关注 0票数 1

在我的视图中有以下几行:

代码语言:javascript
运行
复制
 //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}}

在我的控制器中,我在动作中

代码语言:javascript
运行
复制
 actions: {
    increase: function() {
        var inputField = $("#qnty-bulk");  //how can I access the Ember.TextField here for each of my items in the {{#each item in controller.content}}??
        var inputValue = inputField.val();
        inputValue = (inputValue != null && inputValue != '' ? (isNaN(inputValue) ? 0 : inputValue) : 0);
        inputValue++;
        console.log(inputField.val());
        inputField.val(inputValue);
    },

我想在每次单击向上按钮时将文本字段的值增加1,如何做到这一点?我可以使用jquery吗?

EN

Stack Overflow用户

发布于 2013-11-11 16:32:42

您可以使用jQuery。但我认为您忽略了数据绑定的概念。

您使用item.qnty属性为TextField创建了一个值绑定。

您的递增函数将如下所示:

代码语言:javascript
运行
复制
actions: {
    increase: function() {
        var quantity = this.get('model.item.qnty');
        this.set('model.item.qnty', quantity++);
    },
}

您甚至可以使用快捷方式函数:

代码语言:javascript
运行
复制
actions: {
    increase: function() {
        this.increaseProperty('model.item.qnty');
    },
}

TextField将自动检测到item.qnty已更改,并更新Ember中的值。

您不应该使用Ember框架以外的任何其他方法来更新Ember值。这样做可能会导致Ember应用程序崩溃,或者在这种情况下,无法按预期工作。

根据您的评论进行编辑。

您当前的哈佛商学院:

代码语言:javascript
运行
复制
{{#each item in controller}}
    <div {{action increase}} ></div>
{{/each}}

这将触发数组控制器中的increase函数,当您想要编辑数组中的项时。

让我们为您的项目指定一个项目控制器:

代码语言:javascript
运行
复制
{{#each item in controller itemController='myItem'}}
    <div {{action increase}} ></div>
{{/each}}

您的MyItemController:

代码语言:javascript
运行
复制
App.MyItemController = Ember.ObjectController.extend({

    actions: {
        increase: function(){
            this.increaseProperty('model.qnty');
        }
    }
})

这将触发项目控制器中的increase函数,您可以在其中直接访问您的项目。有一个用于数组的ArrayController和一个用于数组中的项的ObjectController总是很好的。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19901618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档