簡體   English   中英

Javascript 僅適用於 alert() 和 Debug 模式

[英]Javascript works only with alert() and Debug mode

我在 Chrome 自動填充字段中遇到了這個問題,其中 Chrome 忽略了“autocmplete="false|off|others"”。 我還嘗試了隱藏的假字段, thisthis等等。 它似乎不起作用。字段變為黃色並包含密碼,為此我決定使用以下 CSS 規則:

input:-webkit-autofill, textarea:-webkit-autofill, select:-webkit-autofill 
{
    -webkit-box-shadow: 0 0 0 1000px white inset !important;
}

使用 javascript:

$(window).load(function() {
    console.log('Inside JQuery Window.Load...');
    if(document.querySelector("input:-webkit-autofill")!= null) {
        document.querySelector("input:-webkit-autofill").value = "";
    }
    console.log('Autofill value(Before Alert): ' + document.querySelector("input:-webkit-autofill"));
    alert('An alert...');
    if(document.querySelector("input:-webkit-autofill")!= null) {
        document.querySelector("input:-webkit-autofill").value = "";
    }
    console.log('Autofill value(After Alert): ' + document.querySelector("input:-webkit-autofill"));
});

手動覆蓋該字段。

以下是上述腳本的日志輸出:

Inside JQuery Window.Load...
XXX:1324 Autofill value(Before Alert): null
XXX:1329 Autofill value(After Alert): [object HTMLInputElement]

當它在調試模式下運行時:

Inside JQuery Window.Load...
XXX:1324 Autofill value(Before Alert): [object HTMLInputElement]
XXX:1329 Autofill value(After Alert): [object HTMLInputElement]

我嘗試了以下排列:

$('input:-webkit-autofill').each(function(){...});
window.document.querySelector("input:-webkit-autofill");
<body onload="chromeCheckAutofill();" />
<script>
    function checkAutofill() {
        if(document.querySelector("input:-webkit-autofill")!= null) {
             document.querySelector("input:-webkit-autofill").value = "";
        }
    }
</script>

這是有問題的領域:

<input name="MY_REFNO" id="MY_REFNO" style="WIDTH: 180px" maxlength="16" onkeypress="upperAlphanumberHandler(event)" onblur="upperAlphanumberChecker('MY_REFNO')" value="" autocomplete="off">

我究竟做錯了什么? 我該如何解決...

要避免 Google Chrome 自動填充,請嘗試:

<input type="password" placeholder="Type your password" onfocus="this.removeAttribute('readonly');" readonly />

如果填充是只讀的,Chrome 不會應用自動填充。

無論如何,您可以使用以下css為名稱屬性值為“pass”的輸入字段更改自動填充顏色(黃色到白色):

input[name="pass"]:-webkit-autofill {
-webkit-text-fill-color: #838B95 !important;
webkit-box-shadow: 0 0 0px 1000px white inset !important; //background
}

提示 盡可能多地使用 css 選擇器始終是一個很好的最佳實踐,以避免其他不需要的 css 規則導致 css 過載。 (即引導程序)。

我認為您沒有在代碼中為文本框提供類型,例如 type="textbox"

<input name="MY_REFNO" id="MY_REFNO" style="WIDTH: 180px" maxlength="16" onkeypress="upperAlphanumberHandler(event)" onblur="upperAlphanumberChecker('MY_REFNO')" value="" autocomplete="off">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM