2017-03-21 32 views
1

我想删除行使用jQuery阿贾克斯,在我看来,我呼吁Ajax控制器js函数/动作错误:语法错误,无法识别的表达当返回HTML

<a href="javascript:void(0);" onClick="markup2('@Url.Action("ChannelDelete", "MyInfo", new {area = ""})');" class="btn btn-lg btn-danger" style="margin-bottom: 15px;">Delete</a> 
function markup2(e) { 
    $(function() { 
    $("#LoadPages").empty(); 
    $("#LoadPages").html('<div style="padding: 20px 15px"><div class="row"><div class="center-block col-md-1" style="float: none;"><img src="/Upload/pageLoader.gif" alt="loading..." /></div></div></div>'); 

    $.ajax({ 
     type: "POST", 
     url: e 
    }).done(function(data) { 
     $("#LoadPages").load(data); 
    }); 
    return false; 
    }); 
} 

上述功能它也没关系,获取网址并发送AJAX POST并获取数据; 当返回数据(HTML),并插入$("#LoadPages")元素(("#LoadPages").load(data)

让我一个错误:

Error: Syntax error, unrecognized expression:

<h2>my channels</h2> 
    <table class="table"> 
     <tr> 
      <th> 
       channel name 
      </th> 
      <th> 
       name 
      </th> 
      <th> 
       UserSelectedChannelJoinDate 
      </th> 
      <th></th> 
     </tr> 
    </table> 

我不知道为什么得到错误

回答

2

首先,你应该使用不引人注意的事件处理程序,而不是过时的on*事件属性。

其次,你的问题是由于使用load()。该方法旨在接收一个URL以发出一个返回HTML的AJAX请求,然后将其设置为指定元素的内容。但是,你的代码是给出一个HTML字符串,而不是一个URL,因此错误。

为了解决这个问题,使用html()代替load()

<a href="#" data-action="@Url.Action("ChannelDelete", "MyInfo", new {area = "" })" class="btn btn-lg btn-danger">Delete</a> 
$(function() { 
    $('.btn').click(function(e) { 
    e.preventDefault(); 

    $("#LoadPages").html('<div style="padding: 20px 15px"><div class="row"><div class="center-block col-md-1" style="float: none;"><img src="/Upload/pageLoader.gif" alt="loading..." /></div></div></div>'); 

    $.ajax({ 
     type: "POST", 
     url: $(this).data('action') 
    }).done(function(data) { 
     $("#LoadPages").html(data); 
    }); 
    }); 
}); 

我也强烈建议您使用外部的样式表,而不是在你的HTML的style属性。

+0

tan Q男人,3天后救我:d –

+0

如何在on处理程序中添加参数? – Muflix

相关问题