如何使 Javascript AJAX 函数(带 JQuery)与 onclick 一起使用?
我想知道最好的方法是什么,创建一个通用的 javascript 函数来发出 AJAX 请求并使用“onclick”调用它,请记住,我需要加载的 ajax 结果才能仍然使用此函数。另外,我正在使用 JQuery 和 PHP。大致如下:
<a href="#" onclick="postLike(<?php echo $post_id; ?>);">Like Post</a>
<script>
function postLike(post_id){
$.ajax({
async:true,
dataType:"html",
position:"html",
success:function (data, textStatus) {
$("#post-"+post_id+"-like").html(data);
},
url: "domain\/likes\/like\/"+post_id
});
return false;
}
</script>
不过似乎对我不起作用。
谢谢!
I was wondering what the best way was the handle creating a generic javascript function that makes an AJAX request and calling it with 'onclick', keeping in mind I need the loaded ajax results to still work with this function. Also, I'm using JQuery and PHP. Something along the lines of:
<a href="#" onclick="postLike(<?php echo $post_id; ?>);">Like Post</a>
<script>
function postLike(post_id){
$.ajax({
async:true,
dataType:"html",
position:"html",
success:function (data, textStatus) {
$("#post-"+post_id+"-like").html(data);
},
url: "domain\/likes\/like\/"+post_id
});
return false;
}
</script>
Doesn't seem to work for me though.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定你的问题是什么,但这就是我处理它的方式。请注意,这可以避免帖子 ID 为字母数字时出现的问题,因为它将其附加到 href 并从那里提取。请注意,它一开始是隐藏的,并且仅在启用 JavaScript 时才显示(无论如何,这是使其工作所必需的)。
要找到您的问题,您应该考虑添加错误处理程序并使用 Firefox/Firebug 查看发出的请求(和响应)。
支持支持 javascript/非 javascript 的浏览器的替代方案
注意:您的后端代码需要能够区分 AJAX 和非 AJAX 请求。请注意,您可以使用 X_REQUESTED_WITH 标头 (HTTP_X_REQUESTED_WITH) 来执行此操作进行 AJAX 调用时由 jQuery 添加。欺骗是有可能的,所以要小心如何使用此检查——比如不要将任何身份验证或授权决策基于它。当您收到 AJAX 请求时,只需返回 HTML 片段即可。对于非 AJAX,您需要再次渲染整个页面。
I'm not sure what your problem is, but this is how I would handle it. Note that this avoids the issue when the post id is alphanumeric since it appends it to the href and extracts it from there. Note that the it starts hidden and is displayed only if javascript is enabled (which is required to make it work anyway).
To find your problem you should consider adding an error handler and using Firefox/Firebug to view the requests (and responses) as they are made.
An alternative to support both javascript/non-javascript enabled browsers
Note: your backend code needs to be able to distinguish between AJAX and non-AJAX requests. Note you can do this using the X_REQUESTED_WITH header (HTTP_X_REQUESTED_WITH) added by jQuery when making AJAX calls. It is possible to spoof so be careful how you use this check -- like not basing any authentication or authorization decisions on it. When you get an AJAX request, just return the HTML snippet. For non-AJAX you'll need to render the entire page again.
最好的方法是通过 jQuery .bind、.live 或 .delegate 函数将函数绑定到 onclick(或 jQuery 中的 .click)事件。
The best way is binding functions to onclick (or .click in jQuery) event by jQuery .bind, .live or .delegate functions.
首先,如果你使用jQuery,则不需要使用锚标记的onclick属性(感觉很唐突)。尝试这个语法:
First of all if you are using jQuery, you don't need to use onclick attribute of anchor tag(it feels very obtrusive). try this syntax:
以下是我通常解决此问题的方法:
在 PHP 中,当您需要循环出帖子并生成链接时,为锚点分配唯一的 id,如下所示:
然后在 JS 代码中,您可以执行以下操作:
< strong>注意
href="javascript://"
因为我不希望浏览器滚动到顶部PHP
JS
有很多方法可以完成任务。这些只是我的 2 美分
干杯
Here is what I normally do to fix this problem:
In your PHP, when you need to loop out the posts and generate the links, assign unique ids for the anchors, like this:
Then in your JS code, you can do this:
Note
href="javascript://"
because I don't want the browser to scroll to topPHP
JS
There are many ways to complete the task. These are just my 2 cents
Cheers