问题
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