与PreventDefault()相反:继续ActionLink逻辑

时间:2014-05-05 20:13:09

标签: ajax json preventdefault

<%: Html.ActionLink("Print", "Print", "Print", New With {.id = Model.ID}, New With {.target = "_blank", .class = "print"})%>

一旦调用preventDefault,如何返回我的actionlink的功能? (返回时没有打开_blank页面)

   $('#NameOfButton').bind('click', function (e) {
                   e.preventDefault(); // Stop click event

                    //Gather Data
                    var data = $(this).parents('form').first().serialize();

                    //Check Data , if saved to DB continue Click functionality
                    $.ajax({
                        url: '<%:Url.Action("Get")%>',
                        cache: false,
                        type: 'POST',
                        data: data,
                        success: function (result) {
                            if (result.Success == true) {
                                //return;
                                return true;
                            } else {
                                //do nothing
                                console.log('false');
                            }
                        }
                    });
                });

解决方案:

 $('#NameOfButton').bind('click', function (e) {
                   e.preventDefault(); // Stop click event

                    //Gather Data
                    var el= $(this);
                    var data = $(this).parents('form').first().serialize();

                    //Check Data , if saved to DB continue Click functionality
                    $.ajax({
                        url: '<%:Url.Action("Get")%>',
                        cache: false,
                        type: 'POST',
                        data: data,
                        success: function (result) {
                            if (result.Success == true) {
                            window.location.href = el.attr('href');
                            } else {
                                //do nothing
                            }
                        }
                    });
                });

1 个答案:

答案 0 :(得分:1)

您可以再次触发事件:

 $('#NameOfButton').bind('click', function (e, skip) {
     if (skip) return; // check param

     e.preventDefault(); // Stop click event

     //Gather Data
     var data = $(this).parents('form').first().serialize();

     var el = $(this);
     //Check Data , if saved to DB continue Click functionality
     $.ajax({
         url: '<%:Url.Action("Get")%>',
         cache: false,
         type: 'POST',
         data: data,
         success: function (result) {
             if (result.Success == true) {
                 console.log('true');
                 el.trigger('click', [true]); // trigger same event with additional param
             } else {
                 //do nothing
                 console.log('false');
             }
         }
     });
 });