使用 ajax、php 和 jQuery 更改 DIV 内容 [英] Change DIV content using ajax, php and jQuery
本文介绍了使用 ajax、php 和 jQuery 更改 DIV 内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一些数据库文本的 div:
I have a div which contains some text for the database:
<div id="summary">Here is summary of movie</div>
和链接列表:
<a href="?id=1" class="movie">Name of movie</a>
<a href="?id=2" class="movie">Name of movie</a>
..
流程应该是这样的:
- 点击链接
- Ajax 使用链接的 url 将数据通过 GET 传递到 php 文件/同一页面
- PHP 返回字符串
- div 改成这个字符串
推荐答案
<script>
function getSummary(id)
{
$.ajax({
type: "GET",
url: 'Your URL',
data: "id=" + id, // appears as $_GET['id'] @ your backend side
success: function(data) {
// data is ur summary
$('#summary').html(data);
}
});
}
</script>
并在列表中添加 onclick
事件
And add onclick
event in your lists
<a onclick="getSummary('1')">View Text</a>
<div id="#summary">This text will be replaced when the onclick event (link is clicked) is triggered.</div>
这篇关于使用 ajax、php 和 jQuery 更改 DIV 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文