通过javascript提交时,不提示IE7表单输入记住密码的提示
时间:2020-03-06 14:59:04 来源:igfitidea点击:
我有一个网站,我们使用Javascript提交登录表单。在Firefox上,它提示用户记住密码,但在IE7上则不会。
经过一番研究后,看来只有在通过Submit控件提交表单时,才在IE7中提示用户。我创建了一些示例html来证明是这种情况。
<html> <head> <title>test autocomplete</title> <script type="text/javascript"> function submitForm() { return document.forms[0].submit(); } </script> </head> <body> <form method="GET" action="test_autocomplete.html"> <input type="text" id="username" name="username"> <br> <input type="password" id="password" name="password"/> <br> <a href="javascript:submitForm();">Submit</a> <br> <input type="submit"/> </form> </body> </html>
href链接没有得到提示,但IE7中将提供"提交"按钮。两者都可以在Firefox中使用。
我无法使用提交按钮使网站的样式看起来相同,有人知道如何通过Javascript提交时显示记住密码提示吗?
解决方案
我们是否尝试在href中放入url并添加了click事件处理程序以提交表单,并从click处理程序中返回false,以使该URL不被导航到。
或者通过JavaScript触发隐藏的提交按钮?
为什么不尝试以这种方式钩住表单提交?
<html> <head> <title>test autocomplete</title> <script type="text/javascript"> function submitForm() { return true; } </script> </head> <body> <form method="GET" action="test_autocomplete.html" onsubmit="return submitForm();"> <input type="text" id="username" name="username"> <br> <input type="password" id="password" name="password"/> <br> <a href="#" onclick="document.getElementById('FORMBUTTON').click();">Submit</a> <br> <input id="FORMBUTTON" type="submit"/> </form> </body> </html>
这样,无论单击链接还是按下提交按钮(或者按下回车键),都将调用函数,并且可以通过返回false来取消提交。这可能会影响IE7解释表单提交的方式。
编辑:我建议始终以这种方式钩住表单提交,而不是在表单对象上调用Submit()。如果调用submit(),则不会触发表单对象的onsubmit。
我们可以尝试使用HTML <button>标记代替链接或者提交按钮。
例如,
<button type="submit">Submit</button>
与标准的<input type =" submit">相比,<button>标记的样式容易得多。有一些跨浏览器的怪癖,但并非不可克服。
可以在粒子树上找到有关<button>的使用的非常出色的文章:重新发现button元素