【发布时间】:2013-02-02 22:21:34
【问题描述】:
在下面的代码中,我有几个超链接。我希望每当我单击任何超链接时都会发出 ajax 请求并得到响应 通过替换超链接在相应超链接的父 div 中呈现。我在谷歌上搜索并尝试了几种不同的代码,但没有得到响应 在相应超链接的父 div 中呈现。我检查了响应及其正常进行。 你可以找到我的一些尝试以评论形式评论。
<html>
<head>
<script type="text/javascript" src="js/stdlib/jquery-1-4-2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('[id^=idclick]').click(function(){
alert("click");
var tempid=this.id;
//var divtemp=tempid.closest("div").attr("id").html(result);
var divtemp=tempid.closest("div");
$.ajax({
url: this.href,
success: function(result) {
//alert(this.href);
//alert(divtemp);
//$("#iddiv").html(result);
//this.parentNode.html(result);
//var id =
//var aid=this.id;
//aid.closest("div").attr("id").html(result);
divtemp.html(result);
}
});
return false;
});
});
</script>
<style type="text/css">
testcss {
}
</style>
</head>
<body>
<div id="iddivone"><a id="idclick" href="TestCodeServlet?page=1">click here</a>Old data</div>
<div id="iddivtwo"><a id="idclickone" href="TestCodeServlet?page=1">click here</a>Old data</div>
<br>
<div id="iddivthree"><a id="idclicktwo" href="TestCodeServlet?page=1">click here</a>Old data</div>
<br>
<div id="iddivfour"><a id="idclickthree" href="TestCodeServlet?page=1">click here</a>Old data</div>
</body>
</html>
请指导我解决这个问题。我知道这是一个很小的问题,但我仍然没有得到解决。我确定我犯了一些愚蠢的错误。
请各位朋友指导一下! 谢谢!
【问题讨论】:
-
你的 ajax 调用的
dataType是什么?是html还是json?? -
@BojanKovacevic 谢谢!先生您的评论。 “为什么不给所有 div 元素其他 id ?”它的打字错误,我有正确的。 “然后使用 $(id).html("") 其中 id 可以是 this.id” 我无法指定 div 的 id,因为我希望响应应该在单击其超链接的 div 中呈现。所以首先我要找出点击了超链接的div。
-
为什么所有 div 都使用相同的 id? ID(与类元素不同)应该是唯一的。为什么不给所有 div 元素其他 id 然后使用 $(id).html("") 其中 id 可以是 $(this).parent().attr("id")?
-
我更新了我的评论。使用 $(this).parent().attr("id");
标签: jquery ajax jquery-selectors