在 Sencha Touch 中,如何使用从表单字段收到的输入进行计算?

发布于 2024-12-29 19:52:11 字数 1234 浏览 3 评论 0原文

假设我有一个这样的表单:

var Sum1 = new Ext.form.FormPanel({
    id: 'Sum1',
    items: [{
        xtype: 'fieldset',
        title: 'Step 1: Calculating The Sum',
        items: [{
            xtype: 'numberfield',
            name: 'num1',
            label: 'First Number'
        },{
            xtype: 'numberfield',
            name: 'num2',
            label: 'Second Number'
        },{
            xtype: 'numberfield',
            name: 'num3',
            label: 'Third Number'
        },{
            xtype: 'numberfield',
            name: 'num4',
            label: 'Fourth Number'
            },{
            xtype: 'button',
            name: 'CalcSum',
            ui: 'Confirm',
            text: 'Calculate Sum',
            handler: function() {
                Ext.getCmp('Total').setValue('1');
            }
        }]
    },{
    xtype: 'fieldset',
    title: 'Calculated Total',
    items: [{
        xtype: 'textfield',
        name: 'Total',
        id: 'Total',
        placeHolder: 'Your Calculated Total'
    }]
});

我想要做的是在某种方法中使用值num1-4,对它们求和并返回一个值 - 然后在我的文本字段Total中设置该值

我对 Sencha Touch 非常陌生,并且正在使用 Sencha Touch 2.0,因此希望得到任何帮助。

谢谢。

Say I have a form like this:

var Sum1 = new Ext.form.FormPanel({
    id: 'Sum1',
    items: [{
        xtype: 'fieldset',
        title: 'Step 1: Calculating The Sum',
        items: [{
            xtype: 'numberfield',
            name: 'num1',
            label: 'First Number'
        },{
            xtype: 'numberfield',
            name: 'num2',
            label: 'Second Number'
        },{
            xtype: 'numberfield',
            name: 'num3',
            label: 'Third Number'
        },{
            xtype: 'numberfield',
            name: 'num4',
            label: 'Fourth Number'
            },{
            xtype: 'button',
            name: 'CalcSum',
            ui: 'Confirm',
            text: 'Calculate Sum',
            handler: function() {
                Ext.getCmp('Total').setValue('1');
            }
        }]
    },{
    xtype: 'fieldset',
    title: 'Calculated Total',
    items: [{
        xtype: 'textfield',
        name: 'Total',
        id: 'Total',
        placeHolder: 'Your Calculated Total'
    }]
});

What I want to do is to use the values num1-4 in some method that sums them and returns a value - which is then set in my textfield Total.

I am very new to Sencha Touch, and am using Sencha Touch 2.0, so would love any assistance.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2025-01-05 19:52:11

我会听 blur 方法。此方法将在您的文本字段失去焦点后调用。然后你可以做你的事情并将其吐出在 total 字段中吗?

请参阅 API:http://docs.sencha.com /touch/2-0/#!/api/Ext.field.Text

也许:

blur: function() {
  // code here
}

或者(我认为更好)

blur: yourFunctionHere()

更新 2012-01-30:
您可以在同一个文件中定义它,甚至可以在每个字段中定义它:

items: [{
        xtype: 'numberfield',
        name : 'num1',
        label: 'First Number',
        blur : function() {
             console.log('You blurred me!');
             // get current value of total field and sum it with me (if I am not null!)
        }
    },{

更好的方法是定义一个函数,您可以在同一个文件中执行它;

items: [{
        xtype: 'numberfield',
        name : 'num1',
        label: 'First Number',
        blur : doCalculateTotals()
    },{

I would listen to the blur method. This method will be called after your textfield loose focus. Then you can do your thing and spit it out in the total field?

See API: http://docs.sencha.com/touch/2-0/#!/api/Ext.field.Text

Perhaps:

blur: function() {
  // code here
}

or (I think better)

blur: yourFunctionHere()

UPDATE 2012-01-30:
You can define this in the same file, or even per field:

items: [{
        xtype: 'numberfield',
        name : 'num1',
        label: 'First Number',
        blur : function() {
             console.log('You blurred me!');
             // get current value of total field and sum it with me (if I am not null!)
        }
    },{

Better way is to defin one function, you can do it in the same file;

items: [{
        xtype: 'numberfield',
        name : 'num1',
        label: 'First Number',
        blur : doCalculateTotals()
    },{
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文