![](/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.