2016-03-02 107 views
3

我有一個問題,只需清除TextBox文本...我正在使用asp.net和位JS的項目。清除TextBox.Text C#/ JS

但是,看起來Mozzila對我的代碼很有幫助,現在它正在擾亂我。

的想法很簡單......用戶可以登錄,並一如既往mozzila問「保存憑據」這是很好的

enter image description here

但是,如果用戶忘記了密碼,選擇鏈接,我送他與URL的郵件,他又回到了新的網頁,其中有文本框

enter image description here

,由於某種原因Mozzila看到我的第一個TextBox的一個是「記憶憑據」

enter image description here

所以我想接下來的事情就

<script type="text/javascript"> 
    $(document).ready(function() { 

     document.getElementById("MainContent_txtNewPass1").value = ""; 
     document.getElementById("MainContent_txtNewPass2").value = ""; 

    }); 

</script> 

而且

protected void Page_Load(object sender, EventArgs e) 
{ 
    txtNewPass1.Attributes["AUTOCOMPLETE"] = "off"; 
    txtNewPass2.Attributes["AUTOCOMPLETE"] = "off"; 

    txtNewPass1.Text = ""; 
    txtNewPass2.Text = ""; 
} 

哪個沒有工作,因爲我不會問這個問題......那些編號asp:文本框是不同的,我不明白爲什麼會發生這種情況或如何阻止它發生?

編輯

我相信它與Mozzila憑證保存的問題,因爲一旦我刪除保存的密碼,問題消失了。但在一天結束的時候,我只是想清除TextBox.Text

SOLUTION

如果有人得到了同樣的問題...我解決了它,但使用

window.onload = function() { 
    document.getElementById("MainContent_txtNewPass1").value = ""; 
    document.getElementById("MainContent_txtNewPass2").value = ""; 
} 

相反$ (文件)。就緒(函數(){

+0

你不會相信它...簡短的是,你應該包括第三,在視覺上隱藏的密碼字段('型=「密碼」風格='display:none'name ='ignoreMe'')在您的實際密碼和確認字段之前... –

+0

原因是瀏覽器*太聰明*,可以自由忽略'autocomplete'屬性的密碼,擁有「更安全的網絡」的目標 - https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion –

回答

2

試試這個答案,語法是你有什麼稍有不同:https://stackoverflow.com/a/9686424/5013141

而不是

txtNewPass1.Attributes["AUTOCOMPLETE"] = "off"; 

試試這個:

txtNewPass1.Attributes.Add("autocomplete", "off"); 
+0

謝謝Michael ... I找到你的鏈接的答案! – Veljko89