在递归调用jquery之后,控件不起作用

时间:2014-06-25 21:27:40

标签: javascript jquery asp.net-mvc

我在模态对话框中打开mvc视图。   我正在尝试进行递归调用,我面临的问题是:在递归调用之后,视图正确加载,但视图上没有任何控件工作:

在Main.js中:

$(function () {
  $(document).on('click', '.ddlCart li', Mod.Carts);
}

Carts.js:

 var Mod = Mod || {};
    Mod.Carts = function (e) {
                var ddlselectedVal = $(this).attr('id');
                var selectedListinsCount = selected_Listings.length;
                var SelectedMlsnums = selected_Listings.join();
                var agentId = $("#AgentId").val();

                var Action;

                var EnvironmentURL = $("#EnvironmentURL").val();


                var postData = { AgentId: agentId, Mlsnums: SelectedMlsnums, ActionTypeValue: “PreAddToCart” };

                var close = function (event, ui) {
                     $('#dvModalDialog').dialog("close");
                              }
                var open = function (event, ui) {    

                    var url = EnvironmentURL + "MLSReports/Stats/SearchContacts";


                    $("#btncart_cancel").on("click", function () {
                        $('#dvModalDialog').dialog("close");
                    });

                    $("#btncart_submit").on("click", function () {

                        var url = EnvironmentURL + "MLSReports/Stats/Cart";

                        //Send the data using post and put the results in a div                   
                        $.post(url, {
                            AgentId: agentId, Mlsnums: SelectedMlsnums, ActionTypeValue: "AddToCart"
                        },
                            function (data) {
                                // Replace current data with data from the ajax call to the div.         
                                $("#dvModalDialog").empty().append(data);
                            });                 

                    });

                    $("#lnkCreateNewcart").on("click", function () {

                        var url = EnvironmentURL + "MLSReports/Stats/Cart";
                        //Send the data using post and put the results in a div                   
                        $.post(url, {
                            ActionTypeValue: "preAddorEditContact"
                        },
                            function (data) {
                                //debugger;
                                // Replace current data with data from the ajax call to the div.         
                                $("#dvModalDialog").empty().append(data);


                                $("#btnCancelContact").on("click", function () {
                                      ////********** replace the view (Contact) with the view (Cart).
    // In the cancel event I am loading the previous page.I am having problem here. after a recursive call none of the controls work.**  

                            // rd.open();
this.Mod.Carts();



                                  }); 
                           });
                    });

                    };
                    if (ddlselectedVal == "AddtoCart") {
                        var rd = Mod.ReportsDialog({ title: 'Add To Cart', close: close, open: open });
                        rd.url = EnvironmentURL + "/MLSReports/Stats/Cart";
                        rd.targetElement = '#dvModalDialog'// '#dvSendEmail'
                        rd.formName = '#frmCart'
                        rd.postData = postData
                        rd.open();
                    }

                };

1 个答案:

答案 0 :(得分:1)

使用this时,引用函数内部this.Mod.Carts();的值将不同。在调用this函数时,您应该使用此方案中的调用将Carts的值绑定到正确的值。

$("#btnCancelContact").on("click", function () {
 Mod.Carts.call(this);
});