Odoo JavaScript在重新加载div onchange事件后不再触发

rjjhvcjd  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(59)

这是Odoo 10.0前端。在我的“网站”中,当用户选择一些字段时,我会动态加载div,这些div包含复选框和输入框。
当用户更改它们时,我有一些在DB上写入新值的函数。我试图只重新加载包含所有行而不是所有页面的div,但如果我使用

$('#container_lines').load(location.href + " #container_lines");

字符串
而不是

window.location.reload();


如果我再次点击复选框,onchange事件不会被触发!
这里是JavaScript

odoo.define('website_sale_order_checklist.checklist_line', function (require) {
        'use strict';

        var ajax = require('web.ajax');
        var core = require('web.core');
        var session = require('web.session');
        var Model = require('web.Model');
        var _t = core._t;

        //var body = $("body");

        //IF some ajax start the windows show a loading bar
        $(document).on({
            ajaxStart: function () {
                $("body").addClass("loading");
            },
            ajaxStop: function () {
                $("body").removeClass("loading");
            }
        });

        console.debug('website_sale_order_checklist');
        $(document).ready(function () {
            //If user select another stage save it
            function update_line_value(checklist_line_id, checklist_line_view_id, checklist_line_view_type) {

                //Take the id of the checklist line int
                var checklist_line_id_int = Number(checklist_line_id);
                //Take the value of the input tag for text and value

                //take the model
                var Checklists = new Model('sale.order.checklist.line');

                //if the selected view is a checklist, check is checked or not
                if (checklist_line_view_type === "checkbox") {
                    var checkboxValue = false;
                    if ($('#' + checklist_line_view_id).prop('checked') === true) {
                        checkboxValue = true;
                    } else {
                        checkboxValue = false;
                    }

                    if (checklist_line_view_id.match(/answer_yes_type/gi)) {
                        console.debug('Write on yes_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_yes: checkboxValue}]
                        ).then(function () {
                            //Call a method in backend that manage the subsections related to a section
                            Checklists.call('subsections_management_frontend', [checklist_line_id_int]).then(function () {
                                window.location.reload();
                            });
                        });
                    } else if (checklist_line_view_id.match(/answer_no_type/gi)) {
                        console.debug('Write on no_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_no: checkboxValue}]
                        ).then(function () {
                            //Reload full page
                            $('#container_lines').load(location.href + " #container_lines");
                        });
                    } else if (checklist_line_view_id.match(/answer_undecided_type/gi)) {
                        console.debug('Write on undecided_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_undecided: checkboxValue}]
                        ).then(function () {
                            //Reload
                            window.location.reload();
                            //$('#container_lines').load(location.href + " #container_lines");
                        });
                    }
                } else if (checklist_line_view_type === "text") {

                    var value = $('#' + checklist_line_view_id).val();

                    if (checklist_line_view_id.match(/answer_text_type/gi)) {
                        console.debug('Write on answer_text_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_text: value}]
                        ).then(function () {
                            //Reload full page
                            window.location.reload();
                        });
                    }

                } else if (checklist_line_view_type === "number") {

                    var value = $('#' + checklist_line_view_id).val();
                    if (checklist_line_view_id.match(/answer_value_type/gi)) {
                        console.debug('Write on answer_value_type');
                        //call method that write the new value for checkbox
                        Checklists.call('write', [checklist_line_id_int, {answer_value: value}]
                        ).then(function () {
                            //Reload full page
                            window.location.reload();
                        });
                    }
                }

            }

            //Intercept witch row and view is clicked
            $('.section_line').on("change", "input, textarea", function () {

                //Get row id of checklist
                var checklist_line_id = $(this).parent().parent().attr('id');
                //Get the id of clicked element
                var checklist_line_view_id = $(this).attr('id');
                //Get the type of clicked element
                var checklist_line_view_type = $(this).attr('type');

                console.debug('checklist_line_id: ' + checklist_line_id);
                console.debug('checklist_line_view_id: ' + checklist_line_view_id);
                console.debug('checklist_line_view_type: ' + checklist_line_view_type);

                //Function that store the changed field
                update_line_value(checklist_line_id, checklist_line_view_id, checklist_line_view_type);

            });

            //Intercept if the user click some Sections to add to checklist
            $('.sectionAddRemove').on("click", function () {
                setTimeout(function () {
                    window.location.reload();
                    //$('#container_lines').load(location.href + " #container_lines");
                    //$('#container_sections').load(location.href + " #container_sections");

                }, 100);

                //$('#container_lines').load(location.href + " #container_lines");
            });
        });

    }
);


我的目标是只重新加载包含行的div,而不是整个页面。

k4emjkb1

k4emjkb11#

解决了这个问题。

//Intercept witch row and view is clicked
        $('.section_line').on("change", "input, textarea", function () {

            //Get row id of checklist
            var checklist_line_id = $(this).parent().parent().attr('id');
            //Get the id of clicked element
            var checklist_line_view_id = $(this).attr('id');
            //Get the type of clicked element
            var checklist_line_view_type = $(this).attr('type');

            console.debug('checklist_line_id: ' + checklist_line_id);
            console.debug('checklist_line_view_id: ' + checklist_line_view_id);
            console.debug('checklist_line_view_type: ' + checklist_line_view_type);

            //Function that stores the changed field
            update_line_value(checklist_line_id, checklist_line_view_id, checklist_line_view_type);

        });

字符串
$('.section_line').on("change"....section_line位于div $('#container_lines')内部,因此当重新加载div时,它会丢失处理程序。
只需将$('.section_line').on("change"...更改为$(document).on("change"...或包含container_lines的高容器

相关问题