java - 单击无法正常工作时更改链接文本

标签 java javascript jquery html ajax

此处 当我点击链接时获得适当的 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/

相关文章:

javascript - Vuetify SASS 无法使用 Nuxt 正确编译

javascript - 如何使用 jQuery 变量隐藏 HTML 表格行

javascript - "between"CSS/jQuery 选择器

javascript - 无法解释 String[] 中的空间

java - 如何使用按钮作为 double 据类型将JTextField输入存储为变量?

java - 是否可以实现由给定的 HashSet 实现支持的 MyHashMap?

Javascript 获取方法

javascript - 从表中删除行后动态 ID 不起作用

java - 在java中创建新的Json对象与使用redis中的JSON对象

java - 如何使用 Hibernate 作为非阻塞 ORM 与 Vert.x