I'm new to AJAX. i want to call AJAX function when i click a button using onclick() event. This is the AJAX function called by onclick() event
function onclickFunction(aId){
$.ajax({
type: "get",
url: "like_audio",
data: {
aId:aId
},
success: function (data){
alert(data);
},
error: function (xhr, ajaxOptions, thrownError){
}
});
return false;
}
This is how i'm calling the onclick() event
<a href="" onclick=onclickFunction(id)
I'm new to AJAX. i want to call AJAX function when i click a button using onclick() event. This is the AJAX function called by onclick() event
function onclickFunction(aId){
$.ajax({
type: "get",
url: "like_audio",
data: {
aId:aId
},
success: function (data){
alert(data);
},
error: function (xhr, ajaxOptions, thrownError){
}
});
return false;
}
This is how i'm calling the onclick() event
<a href="" onclick=onclickFunction(id)
Share
Improve this question
edited Feb 6, 2016 at 16:31
rrk
15.9k4 gold badges30 silver badges47 bronze badges
asked Feb 6, 2016 at 16:30
NibrasNibras
3691 gold badge7 silver badges24 bronze badges
1
-
Put quotes arount the attribute value of onclick
onclick="onclickFunction(id)"
– Bernie Commented Feb 6, 2016 at 16:40
2 Answers
Reset to default 6Just get the use return inside onclick
, which will prevent the reload.
<a href="" onclick="return onclickFunction('id')">a</a>
As an alternative you can use
<a href="javascript:onclickFunction('id')">a</a>
<a href="#" onclick="onclickFunction('theid');">Click Me</a>
Will run the function, and pass it the value 'theid'
In your example, you don't have a ple href, and even if it was right, you might not have an ID value to pass to it. if i do this:
Click Me
I am basically saying, there is a variable called id in the global scope, go find its value and pass it to the function - if there is indeed a variable like that on your page, then it will work, however i don't know if there is.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743760842a4502594.html
评论列表(0条)