将jquery函数绑定到事件

时间:2013-12-17 12:31:13

标签: javascript jquery asp.net ajax asp.net-mvc

我想在点击一个锚链接标签时绑定一个jquery函数,但不知道它是如何绑定的,我正在使用带有Ajax的ASP.net MVC并希望在Ajax的帮助下显示数据。请提供一些建议,以便在链接点击上绑定以下功能..谢谢..

$(document).ready(function(){
    $("a.ShowTable").click(function(e){
    var url=this.href;
    $get(url,{},function(data){
        $('#dtable').html(data)
    )};
    e.preventDefault();
});
<ul>

    @foreach(var item in Model)
    {
    <li><a href="@Url.Action("Index","Home",new {id=item.Id})">@item.Id</a><li>
    }
    </ul>

4 个答案:

答案 0 :(得分:1)

您可以使用bind

$(document).ready(function(){
   $( "a.ShowTable" ).bind( "click", function() {
     var url=this.href;
      $get(url,{},function(data){
         $('#dtable').html(data)
      });
       e.preventDefault();
   });
});

答案 1 :(得分:1)

这是您的固定代码:{请注意,我不是您浏览器的控制台...}

$(document).ready(function () {
    $("a.ShowTable").click(function (e) {
        var url = this.href;
        $.get(url, {}, function (data) {
            $('#dtable').html(data);
        });
        e.preventDefault();
    });
});

答案 2 :(得分:1)

首先在脚本上添加jQuery文件

然后写下面的代码

$(document).ready(function(){
    $(document).on("click","a.ShowTable,"function(e){
    e.preventDefault();
    var url=this.href;
    $get(url,{},function(data){
        $('#dtable').html(data)
    });
 });    
}); 

答案 3 :(得分:1)

$(document).ready(function(){
    $("a.ShowTable").click(function(e){
    var url=$(this).attr(href);
    $.get(url,{},function(data){
        $('#dtable').html(data)
    )};
    e.preventDefault();
});

<ul>
    @foreach(var item in Model)
    {
    <li><a href='@Url.Action("Index","Home",new {id=item.Id})' class='ShowTable'>@item.Id</a><li>
    }
    </ul>
相关问题