Chrome浏览器中的意外网页刷新

len*_*ord 6 html javascript jquery google-chrome

我一直面对这种奇怪的行为,现在找不到任何解决方法.

点击时有一个按钮,其中包含某些方法.在Firefox中运行良好.在Chrome中,它只刷新整个页面.

$("#modoComparativa").click(function(){
   if($(this).hasClass("active")){
                $('#histFromDate').attr("placeholder","Date 1");
                $('#histToDate').attr("disabled","disabled").attr("placeholder","Date 2");
                startDatepickerComp();
            }
            else{
                $('#histFromDate').attr("placeholder","Initial date");
                $('#histToDate').attr("disabled","disabled").attr("placeholder","Final date");
                startDatepicker();
            }
            $('#clearDates').attr("disabled","disabled");
            // This function calls another function causing the odd behavior in Chrome
            requestGraph(idDetail, idArea, "", "");
        });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

如果我注释掉除了选择器和对click方法的调用之外的所有内容,则行为是相同的.它刷新了一切.

我无法弄清楚如何调试这个,因为每次按下按钮,整个页面都会刷新,浏览器调试器中没有日志/错误.

任何想法,将不胜感激.

编辑 - 根据请求添加选择器:

<div class="form-group"><button class="tooltip3 btn btn-default glyphicon glyphicon-random" id="modoComparativa" data-toggle="tooltip" title="ACTIVAR COMPARATIVAS" data-placement="bottom"></button>
                            </div>
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 8

在单击回调中添加一个返回false,以防止由于您的html语法而导致的操作(如表单提交或单击锚标记).

$("#modoComparativa").click(function(event){
    event.preventDefault();

    // your existing code
}
Run Code Online (Sandbox Code Playgroud)

编辑:既然你添加了你的HTML ...

如果您的按钮位于表单中,您还可以添加type ="button"属性以防止它提交表单.

<button type="button" class="tooltip3 btn btn-default glyphicon glyphicon-random" id="modoComparativa" data-toggle="tooltip" title="ACTIVAR COMPARATIVAS" data-placement="bottom"></button>
Run Code Online (Sandbox Code Playgroud)

感谢lonesomeday建议http://api.jquery.com/event.preventDefault/

  • `return false` 不是执行此操作的理想方法。正确的方式是`event.preventDefault()`,最好在回调函数的顶部。 (4认同)