passing id to ajax call

安稳与你 提交于 2019-12-12 03:38:18

问题


I have a link like this <a class="viewp" href="#">@data.name</a>

and I would like to call a jquery ajax this way

    $(document).ready(function ()
    {
        $('.viewp').click(function (id)
        {
            var responseUrl="~/click?id="+id;                    
            $.ajax(
            {
                type: "GET",
                data:id,
                url:responseUrl,
                success:success
            });
        });
    });

But I don't know how the id of the @data.name is passed into the jquery function. If I replace the above link's href with href="~/click?id=@data.id" then that is supposed to load the whole page not some specific region and clearly ajax doesn't work also.

[UPDATE]

By id I would mean the id primary key of my sql table and I am using webmatrix to code my simple web page. My database table looks like this create table x(id, name)


回答1:


I haven't got what exactly you mean

if it is like

< a class="viewp" href="#" id="someId" >@data.name< /a>

if you want to get id of it

then

$(this).attr("id");

if you want to get text @data.name

then

$(this).text();




回答2:


You can do something like this:

<a id="@data.id" class="viewp" href="#">@data.name</a>

And then in the function you can get the id:

$('.viewp').click(function()
{
    var id = this.id
}



回答3:


use $(this) to get the currently clicked a tag and then get the id attribute value of that.

 $('.viewp').click(function(){
    var id=$(this).attr("id");   
    var responseUrl="~/click?id="+id 
     //do your ajax call here
 });


来源:https://stackoverflow.com/questions/12546212/passing-id-to-ajax-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!