【问题标题】:Opposite of PreventDefault() : Continuing ActionLink logicPreventDefault() 的对面:继续 ActionLink 逻辑
【发布时间】:2014-05-05 20:13:09
【问题描述】:
<%: 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
                            }
                        }
                    });
                });

【问题讨论】:

    标签: ajax json preventdefault


    【解决方案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');
                 }
             }
         });
     });
    

    【讨论】:

    • 我已经添加了 click 函数参数、el 实例化和success = true 行上的触发器,但是当它再次触发时,skip = undefined。想法?
    • 所以事件函数永远循环?
    • 不,它似乎没有正确地将 $(this) 设置为 el ,在 consoleLog 上它会吐出一些具有真假值的对象。当我对 el = e 做同样的事情时;它为 actionlink 元素吐出正确的格式,但是当它到达 el.trigger('click', [true]); 的行时它显示 Uncaught TypeError: undefined is not a function :所有这些都在第一次迭代中
    • console.log(el) 的确切输出是什么(在分配给$(this) 后立即调用)?
    • 在上面编辑过,作业是我的错。然而,在触发触发后,它会检查跳过变量并返回函数吗? - 我应该将 return 更改为 return true;跳过检查?
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多