web2py:检测下拉小部件中的更改

时间:2019-01-24 02:47:47

标签: python jquery web2py

我们如何从下拉窗口小部件中检测到更改,以便可以执行代码?

loan引用表services。两者都有名为interest_rate的字段。当我通过下拉菜单更改loan.service时,我想将相应的interest_rateservices表反映到loan表。

如何实现?

型号

db.define_table('services',
                Field('service_name',requires=[IS_NOT_EMPTY(),IS_NOT_IN_DB(db,'services.service_name')]),
                Field('service_type','reference service_types',requires=IS_IN_DB(db,db.service_types.id,
                                                                                '%(type_name)s',
                                                                                error_message='not in table',
                                                                                zero=None),
                                                                                ondelete='RESTRICT',
                                                                                ),
                Field('interest_rate','decimal(15,2)',requires=IS_DECIMAL_IN_RANGE(0,100)),
                Field('max_term','integer'),
                auth.signature,
                format='%(service_name)s',
    )

db.define_table('loan',
    Field('service','reference services',requires=IS_IN_DB(db,db.services.id,
                                            '%(service_name)s',
                                            error_message='not in table',
                                            zero=None),
                                            ondelete='RESTRICT',),
    Field('member_id','reference members',requires=IS_IN_DB(db,db.members.id,
                                            '%(member_name)s',
                                            error_message='not in table',
                                            zero=None),
                                            ondelete='RESTRICT',
                                            label='Member'),
    Field('amount','decimal(15,2)',requires=IS_DECIMAL_IN_RANGE(1000)),
    Field('interest_rate','decimal(6,2)',default=10),
    )   

1 个答案:

答案 0 :(得分:0)

通过在前端使用javascript。例如,使用jquery库:

$("select").on("change", function(){

    var selected = $(this).val();

    $.ajax({
        url: "/get_rates",
        data:{
            "selected": selected
        },
        success: function(value_from_url){
            $("div").text(value_from_url);
        }
    });
});