Jquery 使用ajax将链接id传递到php页面,并在另一个div中打开
我有一个index.php页面,其链接如下:Jquery 使用ajax将链接id传递到php页面,并在另一个div中打开,jquery,ajax,jquery-ajaxq,Jquery,Ajax,Jquery Ajaxq,我有一个index.php页面,其链接如下: <a id="link" href="test.php?id=1">Test</a> <div id="container" class="panel-body"> <script type="text/javascript"> $("#link").click(function(){ $.ajax({ type: "get", url: "test.php",
<a id="link" href="test.php?id=1">Test</a>
<div id="container" class="panel-body">
<script type="text/javascript">
$("#link").click(function(){
$.ajax({
type: "get",
url: "test.php",
data: {'data':dataString},
success: function(data){
$('#container').html(data);
}
});
});
</script>
我在同一页上有一个div容器,如下所示:
<a id="link" href="test.php?id=1">Test</a>
<div id="container" class="panel-body">
<script type="text/javascript">
$("#link").click(function(){
$.ajax({
type: "get",
url: "test.php",
data: {'data':dataString},
success: function(data){
$('#container').html(data);
}
});
});
</script>
和ajax jQuery代码在同一页面上,如下所示:
<a id="link" href="test.php?id=1">Test</a>
<div id="container" class="panel-body">
<script type="text/javascript">
$("#link").click(function(){
$.ajax({
type: "get",
url: "test.php",
data: {'data':dataString},
success: function(data){
$('#container').html(data);
}
});
});
</script>
$(“#链接”)。单击(函数(){
$.ajax({
键入:“获取”,
url:“test.php”,
数据:{'data':数据字符串},
成功:功能(数据){
$('#container').html(数据);
}
});
});
我的工作是在test.php的链接中传递上面的id=1,并在div#container的同一页面中显示值
问题是,单击链接会在url test.php?id=1的新窗口中打开一个新页面,而不是在同一页面上显示php的内容
如何在jquery中名为#container的div上显示test.php页面的结果
提前感谢。只需防止链接的默认行为:
$("#link").click(function(event){
event.preventDefault();
$.ajax({
type: "get",
url: "test.php",
data: {'data':dataString},
success: function(data){
$('#container').html(data); //copy and paste for your special case
}
});
});
问题是,单击链接会在url test.php?id=1的新窗口中打开一个新页面,而不是在同一页面上显示php的内容
您需要使用event.preventDefault()
停止锚点的默认行为:
$(“#链接”)。单击(函数(e){
e、 preventDefault();//由于未定义dataString数据,{'data':dataString},它现在在firebug中显示的可能重复项,