2017-01-30 58 views
-3

我遇到了一些JavaScript代码的问题。当我在本地测试网站时它工作正常,但在服务器上不起作用,除非我重新加载页面。代码如下。如果您需要更多详细信息,请告诉我。Javascript在本地工作,但不在服务器上

$(document).ready(function(){ 

$("#header_inbox_bar").click(function() { 

const inboxtopcount = $('#inboxtopcount') 
const badgedanger = inboxtopcount.parent('.badge-danger') 

$.ajax({ 
    type: 'POST', 
    url: 'controllers/ctrl_client_update_inbox_notifications.php', 
    success (res) { 
    if (res) { 

     badgedanger.hide() 
     inboxtopcount.hide() 
    } 
    }, 

}); 
}); 
}); 
+0

同源策略? – Evgeny

+0

你看到任何错误? – jdave

+0

请向我们显示错误 – sumit

回答

0

我的猜测是你的DOM元素没有及时绑定到jQuery。另外,尝试检查你的jQuery的语法错误或任何缺少的语法。

要解决加载时出现的任何绑定问题,请尝试使用jQuery的'on'方法,以便您可以将它传递给#header_inbox_bar元素并在稍后进行绑定。像这样:

$(document).ready(function() { 
 

 
    $('body').on('click', '#header_inbox_bar', function() { 
 

 
     const inboxtopcount = $('#inboxtopcount'); 
 
     const badgedanger = inboxtopcount.parent(); 
 

 
     $.ajax({ 
 
      type: 'POST', 
 
      url: 'controllers/ctrl_client_update_inbox_notifications.php', 
 
      success(res) { 
 
       badgedanger.hide(); 
 
      }, 
 
     }); 
 

 
    }); 
 
});

+0

此代码片段可能只是格式化我的代码... –

+0

哇这是工作正常:)很多谢谢安德烈斯。我有很多要学习:) – pippo

相关问题