javascript - how to call AJAX function in anchor tag - Stack Overflow

I'm new to AJAX. i want to call AJAX function when i click a button using onclick() event. This is

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
Add a ment  | 

2 Answers 2

Reset to default 6

Just 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

相关推荐

  • javascript - how to call AJAX function in anchor tag - Stack Overflow

    I'm new to AJAX. i want to call AJAX function when i click a button using onclick() event. This is

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信