简体   繁体   English

如何在锚标记中调用AJAX函数

[英]how to call AJAX function in anchor tag

I'm new to AJAX. 我是AJAX的新手。 i want to call AJAX function when i click a button using onclick() event. 我想使用onclick()事件单击按钮时调用AJAX函数。 This is the AJAX function called by onclick() event 这是onclick()事件调用的AJAX函数

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 这就是我调用onclick()事件的方式

<a href="" onclick=onclickFunction(id)

Just get the use return inside onclick , which will prevent the reload. 只需在onclick内获取使用返回,即可防止重新加载。

<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' 将运行该函数,并将其值传递给'theid'

In your example, you don't have a comple href, and even if it was right, you might not have an ID value to pass to it. 在您的示例中,您没有comple href,即使它是正确的,也可能没有要传递给它的ID值。 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. 我基本上是说,在全局范围内有一个名为id的变量,去查找其值并将其传递给函数-如果确实在您的页面上有这样的变量,那么它将起作用,但是我不知道如果有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM