2010-11-21 95 views
0

我试图对用户单击执行输入验证。然而,jQuery的click()函数一直工作不稳定;有时它会像我期望的那样工作,有时点击链接除了将视口移动到屏幕顶部之外什么也不做。问题是我不能一致地复制错误;有时会起作用,有时却不起作用。我已经在Windows XP和Mac 9.0上的Firefox 9.0中复制了它,但是,有时该页面可以正常工作,有时不会。有一次,当它加载不正确时,我尝试清除缓存,并再次加载页面,并且页面仍然没有加载。它似乎从没有加载到加载,特别是在对文件进行大的修改之后。jquery click()工作不一致

我在这里设置了一个简化测试用例:http://kburke.org/thesis/consentform1.php。如果表单工作正常,它应该显示一个警告框。这里是代码:

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 

    $("#submitlink").click(function(){ 
     console.log("link clicked"); 
     alert("Clicked!"); 
    }); 
    console.log("loading..."); 
}); 
</script> 
</head> 
<body style="width:35em; margin-bottom:200px;"> 
<input type="text" value="" name="part_name" id="name1" /> 
<input type="text" value="" name = "part_name2" id="name2" /> 
<input type="checkbox" name="accept_1" id="accept_1" />I signify my agreement that all matters and issues mentioned above have been discussed to my satisfaction and agreement.  <br><br> 
<input type="checkbox" name="accept_2" id="accept_2" />I acknowledge that I am 18 years of age or older on the date of signing this consent, and that I have read and understood all of the above.</p> 
<div id="submitlink" style="float:right; color:blue; text-decoration:underline; cursor:pointer;">I agree</div> 
<form method="post" id="agree" action="/thesis/instructions.php"> 
    <input type="hidden" name="id" id="nextpage" value=""> 
    <input type="hidden" name="name" id="the_name" value="" /> 
</form> 
</body> 
</html> 

谢谢你的任何见解,你可能有,我特别是困惑,行为是不一致的。

编辑:有人建议console.log导致的问题,但我在移除控制台语句后仍然有问题。运行Chrome的JS调试器表明,$("#submitlink")调用中的代码在发生问题时根本没有运行。像往常一样,我感谢你的帮助。

+0

产生错误的整页是在这里:http://kburke.org/thesis/consentform.php页面上 – 2010-11-21 04:44:13

+0

链路工作就好在我的Chrome浏览器。 .. – Lukman 2010-11-22 03:17:55

+0

对于我来说,当前版本(它不起作用)是Mac上的Chrome 7.0。我也把它缩小到最小的情况下,它仍然不适合我。 – 2010-11-22 03:22:41

回答

1

我解决了这个问题。 Google Voice Chrome扩展程序正在为该页面上的电话号码添加链接,该链接干扰了jQuery操作。重要的测试,包括删除页面上的元素和禁用Chrome扩展,都验证了扩展名导致了问题。

下次我会试着记住禁用所有扩展以查看是否导致问题。

我在这里提交bug报告:http://code.google.com/p/chromium/issues/detail?id=64019

+0

事实上,我刚才发现,如果您禁用了用于制作可点击数字的功能,问题就会消失。 – robx 2011-05-31 18:51:54

3

我注意到你的代码中有一些console.log()声明。我还注意到,当我打开Firebug时,你的代码工作。当我关闭Firebug时,代码不起作用。

console对象不是JavaScript本身的一部分;它是由Firebug(和许多其他调试工具)创建的,以取代批量调试方法alert。当Firebug关闭时,console.log()行会产生致命错误,从而停止执行JavaScript。

+0

嗯。这似乎解释了它,特别是因为我将通过在Firebug中打开控制台来检查错误,而不会显示错误。 – 2010-11-21 05:15:38

+0

@Kevin,因为所有的浏览器都不支持这个功能,所以它始终可以将consolo.log消息放在try catch块中。 – kobe 2010-11-21 05:25:13

+0

我通常使用'safeLog'函数,使用'if($。isFunction(console.log)){console.log(...); }' – rossipedia 2010-11-21 06:23:07

0

删除的console.log,为的console.log不会是可用的总