我正在调试一个页面,该页面有一个包含文本框和确定按钮的 jquery 对话框。
当用户按下回车键时,页面将重新加载,文本框 ID 和值将作为获取参数传递给页面重新加载。例如
http://example.com?tex_box_id=text_entered_in_text_box
我不知道是什么导致了这种行为,也不知道如何最好地追踪它,因为页面本身正在重新加载。
我试过单步执行所有 jquery 代码,但没有任何运气。我只能假设有人在某个地方附加了一个按键监听器,但我不知道是谁。我知道我可以通过阻止他们运行来解决这个问题,但我仍然很想弄清楚为什么会这样。
请注意,如果您单击“确定”按钮,则不会发生这种情况,只有当您在文本框中按回车键时才会发生这种情况
最佳答案
这将禁止提交页面上的所有表单:
$('form').submit(function() {
return false;
});
虽然这将“解决”您的问题,但您应该专门针对您的表单,并使用自定义 JavaScript 行为来提交数据(例如使用 $.ajax)。
要进行更高级的调试,请使用以下命令:
$(window).keydown(function(event) {
var breakpoint = 1; // Place a breakpoint on this line!
// Then use the "step out of current function" button to
// continue through the JS that gets executed... much of it
// will likely be native code, but if you pay attention to
// the native function names, you should eventually see the
// event that is causing the reload.
});
关于javascript - 如何在 javascript 中跟踪页面重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7823967/