参考:http://raibledesigns.com/rd/entry/browser_based_username_password_autocomplete
1、页面设置一个隐藏表单,action 设置为 javascript:void(0);"
<form method="post" action="javascript:void(0);" style="display:none ">
<input type="text" id="j_username" name="j_username" value=""/>
<input type="password" id="j_password" name="j_password" value=""/>
<input type="submit" value="Login" id="loginBtn"/>
</form>
2、真正要提交的表单元素 设置为 autocomplete=off
DOM.setElementAttribute(edt_username.getElement(), "autocomplete", "off");
DOM.setElementAttribute(edt_password.getElement(), "autocomplete", "off");
3、表单提交前,用隐藏表单记录表单值,并调用隐藏表单的提交
/**
* invoke before actual form submit
*/
public void toFixFFAutocomplete(){
DOM.getElementById("j_username").setAttribute("value",edt_username.getValue());
DOM.getElementById("j_password").setAttribute("value", edt_password.getValue());
clickVirtualFormLogin();
}
public static native void clickVirtualFormLogin() /*-{
// alert("page");
$doc.getElementById("loginBtn").click();
}-*/;
4、页面初始化时,从隐藏表单获取值,填入真正的表单中
private void setFFAutocomplete() {
// delay 500 ms to set username&password
Timer t = new Timer() {
public void run() {
edt_username.setValue(getElementValue("j_username"));
edt_password.setValue(getElementValue("j_password"));
if (edt_password.getValue() != null&&edt_password.getValue().trim().length()>0) {
edt_password.setFocus(true);
}else{
edt_username.setFocus(true);
}
}
};
t.schedule(500);//注意要延时 0.5秒
}