此处 当我点击链接时获得适当的 id 但将状态更改为所有帖子。不在所选项目中
这里是html代码
<div th:each="p : ${posts}">
<div id="para">
<a style="float: right;" href="#" class="decision"
th:text="${p.approved}">Approve</a>
<input type="hidden" class="pid" th:value="*{p.id}" />
</div>
</div>
当我点击批准链接时,id 值是正确的。但更改状态会影响所有其他帖子
这是脚本
<script>
$(document).ready(function() {
$('.decision').click(function(e) {
$.ajax({
type : 'POST',
dataType : 'json',
url : '/RealEstate/ChangeStatus.html',
data : ({
pid :$(this).siblings('.pid').val()
}),
success : function(response) {
alert(response);
$('.decision').html(response);
/* $('.decision').text(response); */
},
error : function() {
alert('Error while request..');
}
});
});
});
</script>
和 Controller
@RequestMapping("/ChangeStatus.html")
@ResponseBody
public String approvDisapprove(@RequestParam("pid") String pid) {
Gson gson = new Gson();
String data = null;
Property property = propertyDAO.findById(Integer.parseInt(pid));
if (property.getApproved() == true) {
property.setApproved(false);
data = gson.toJson("False");
} else {
property.setApproved(true);
data = gson.toJson("True");
}
propertyDAO.update(property);
return data;
}
如果你知道这个问题请分享答案
最佳答案
您需要像下面这样使用this
引用:
<script>
$(document).ready(function() {
$('.decision').click(function(e) {
$.ajax({
type : 'POST',
dataType : 'json',
url : '/RealEstate/ChangeStatus.html',
data : ({
pid :$(this).siblings('.pid').val()
}),
success : function(response) {
alert(response);
$(this).html(response);
^------^ // here use this instead of .decision selector
/* $('.decision').text(response); */
},
error : function() {
alert('Error while request..');
}
});
});
});
</script>
关于java - 单击无法正常工作时更改链接文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23646457/