weixin_33743248 2016-10-10 07:15 采纳率: 0%
浏览 95

jQuery onchange Ajax


i made a function that sends data (ajax) to the database and depending on the response from the server i need to alert a message but it seems like whenvever i change the select option i get the alert message for each change(if i change the select four times when i click i get the alert four times ) , but if i remove my ajax function and replace it simply by an alert i get it once not repeating itself here is my JS

        $('.select_ids').change(function () {
            var id = $(this).val();
            var form = $('#form_widget_ids_' + id);
            var container = form.parent('.ewb_forms');
            var box = container.parent('.edit_widget_box');

            container.children('.selected').fadeOut(300, function () {
                $(this).removeClass('selected');
                form.fadeIn(300, function () {
                    $(this).addClass('selected');
                });
            });
            Widget.updateSSOUrl(box);

            $.ajax({
                type: "POST",
                url: window.location + "",
                data: {'id': id}

            }).done(function (msg) {

                $(".red").on('click', function (evt) {
                    if ('done' == msg) {
                        evt.preventDefault();
                        alert('NOP');
                    }
                })



            });
        });
  • 写回答

1条回答 默认 最新

  • weixin_33726943 2016-10-10 07:20
    关注

    the event that you are binding i think is wrong. For newly append items is better in your case to use

    $(document).on('click', ".red", function (evt) {
    
                    })
    

    And it must be moved outside the ajax success because now you are triggering it every time

    ----- Edited --- If you want just to alert the output of the ajax you dont need the onClick event

    $('.select_ids').change(function () {
                var id = $(this).val();
                var form = $('#form_widget_ids_' + id);
                var container = form.parent('.ewb_forms');
                var box = container.parent('.edit_widget_box');
    
                container.children('.selected').fadeOut(300, function () {
                    $(this).removeClass('selected');
                    form.fadeIn(300, function () {
                        $(this).addClass('selected');
                    });
                });
                Widget.updateSSOUrl(box);
    
                $.ajax({
                    type: "POST",
                    url: window.location + "",
                    data: {'id': id}
    
                }).done(function (msg) {
                        if (msg === 'done') {
                            evt.preventDefault();
                            alert('NOP');
                        }
                });
            });
    

    If you want to show the latest result on a button click you can store the msg on a global variable and on click of a div show that like

    var globalMsg = "";
    $('.select_ids').change(function () {
                var id = $(this).val();
                var form = $('#form_widget_ids_' + id);
                var container = form.parent('.ewb_forms');
                var box = container.parent('.edit_widget_box');
    
                container.children('.selected').fadeOut(300, function () {
                    $(this).removeClass('selected');
                    form.fadeIn(300, function () {
                        $(this).addClass('selected');
                    });
                });
                Widget.updateSSOUrl(box);
    
                $.ajax({
                    type: "POST",
                    url: window.location + "",
                    data: {'id': id}
    
                }).done(function (msg) {
                  globalMsg = msg
                });
            });
    
    $(".div").click(function() { alert(globalMSG); });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line