[英]Jquery/Ajax Done Event Only Running Once
The following code successfully runs and adds new values to the database... 以下代码成功运行并将新值添加到数据库中...
<script type="text/javascript">
$(document).ready(function() {
var i = 0;
$(".testClick").click(function () {
var dbvalue = $(this).data('dbvalue');
var task_id = $(this).data('wrkval');
$.ajax({
type: 'POST',
url: '/tasks/update_sub_task',
dataType: 'json',
data: {
tasks: {
id: task_id,
subtask_id: dbvalue
}
},
}).done(function() {
console.log("SUCCESS");
$("#working_area").html("<%= escape_javascript(render(:partial => 'sub_tasks.js.erb', locals: {items: params[:w]} )) %>");
});
});
});
</script>
<% @days.each do |e| %>
<a id="ID=1" class="testClick" data-dbvalue="<%= e.id %>" data-wrkval="<%= params[:w] %>"><%= e.name %></a>
<% end %>
It logs "SUCCESS" in the console and replaces the current content of the #working_area div with the partial. 它将“ SUCCESS”记录在控制台中,并将#working_area div的当前内容替换为局部内容。
Fantastic... 太棒了...
However when I click the next link, it logs another SUCCESS message but for some reason it doesn't replace the #working_area content again. 但是,当我单击下一个链接时,它会记录另一个SUCCESS消息,但是由于某种原因,它不会再次替换#working_area内容。
Does anyone have any ideas how I can get this to replace/refresh each time a link is clicked? 是否有人对每次单击链接时如何使它替换/刷新有任何想法?
Update - Okay I've got it reloading and I've realised that it because my partial was rendered when the modal was clicked it was fixed "as-of that time". 更新-好的,我已经重新加载它,并且我已经意识到,这是因为在单击模式时我的部分已渲染,因此它是“在那时”修复的。
You should use on
instead like this: 您应该这样使用
on
:
$(document).on("click",".testClick",function (){
..code here...
});
Use an 'on' event. 使用“开”事件。
Replace 更换
$(".testClick").click(function () {
$(“。testClick”)。click(function(){
with 与
$(".testClick").off('click').on('click', function() {
$(“。testClick”)。off('click')。on('click',function(){
Also make sure that #working_area is still valid after replacing the content for the first time. 首次替换内容后,还要确保#working_area仍然有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.