![](/img/trans.png)
[英]How to disable typing but allow copy and paste in a text box in Angular 8?
[英]Disable Copy or Paste action for text box?
我有兩個文本框,我想阻止用戶從第一個(電子郵件)文本框中復制值並將其粘貼到第二個(確認電子郵件)文本框中。
Email: <input type="textbox" id="email"><br/>
Confirm Email: <input type="textbox" id="confirmEmail">
我有兩個解決方案:
關於如何做的任何想法?
用
oncopy="return false"
onpaste="return false"
Email: <input type="textbox" id="email" oncopy="return false" onpaste="return false" ><br/>
Confirm Email: <input type="textbox" id="confirmEmail" oncopy="return false" onpaste="return false">
前任:
<input type="textbox" ondrop="return false;" onpaste="return false;">
在 HTML 中所需的文本框中使用這些屬性。 現在拖放和粘貼功能被禁用。
這是更新的小提琴。
$(document).ready(function(){
$('#confirmEmail').bind("cut copy paste",function(e) {
e.preventDefault();
});
});
這將防止在確認電子郵件文本框中剪切復制粘貼。
希望能幫助到你。
嘗試這個
$( "#email,#confirmEmail " ).on( "copy cut paste drop", function() {
return false;
});
你可以試試這個:
<input type="textbox" id="confirmEmail" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">
更新:接受的答案提供了解決方案,但.on()是從現在開始應該使用的方法。
“從 jQuery 3.0 開始,.bind() 已被棄用。自 jQuery 1.7 起,它被用於將事件處理程序附加到文檔的 .on() 方法取代,因此已經不鼓勵使用它。”
最好的方法是將數據屬性添加到要避免剪切、復制和粘貼的字段(文本框)。
只需為此創建一個方法,如下所示:-
function ignorePaste() {
$("[data-ignorepaste]").bind("cut copy paste", function (e) {
e.preventDefault(); //prevent the default behaviour
});
};
然后,當您添加上述代碼時,只需將數據屬性添加到要忽略剪切復制粘貼的字段。 在我們的例子中,您添加一個數據屬性來確認電子郵件文本框,如下所示:-
Confirm Email: <input type="textbox" id= "confirmEmail" data-ignorepaste=""/>
調用方法 ignorePaste()
因此,通過這種方式,您將能夠在整個應用程序中使用它,您只需在要忽略剪切復制粘貼的地方添加數據屬性
您可能還需要向您的用戶提供警報,表明這些功能對於文本輸入字段已禁用。 這將工作
function showError(){ alert('you are not allowed to cut,copy or paste here'); } $('.form-control').bind("cut copy paste",function(e) { e.preventDefault(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class="form-control" oncopy="showError()" onpaste="showError()"></textarea>
你可以試試下面的代碼
$(document).ready(function() {
$('#email, #confirmEmail').on("cut copy paste drop", function(e) {
e.preventDefault();
return false;
});
});
在文本框中添加以下代碼作為屬性
onpaste="return false" oncut="return false" oncopy="return false" ondrag="return false" ondrop="return false" onselectstart="return false"
例子:
<input type="textbox" id="email" onpaste="return false" oncut="return false" oncopy="return false" ondrag="return false" ondrop="return false" onselectstart="return false" />
請參閱此示例。 您需要綁定事件鍵傳播
$(document).ready(function () {
$('#confirmEmail').keydown(function (e) {
if (e.ctrlKey && (e.keyCode == 88 || e.keyCode == 67 || e.keyCode == 86)) {
return false;
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.