Disable text selection on input fields

做~自己de王妃 提交于 2019-12-10 17:20:08

问题


I am developing a Hybrid application using IBM Worklight, HTML5, CSS3 and javascript. Is there any possible way to disable text selection on input fields?

I have a username and password field on the screen and want to disable the user from selecting text within the input fields. Also, copy paste should be disabled.

Thanks


回答1:


There is an answer already on SO for selecting that will probably give you what you need: Prevent element from taking part in text selection

There are also answers on SO for disabling pasting on some browsers: Disable pasting text into HTML form

Keep in mind though that the above will not work in every browser.




回答2:


To disable pasting text into input field,

$('#myInput').bind('input', function(e) {
    $("#myInput").val("");    //This will reset input value to empty
});



回答3:


you can use below code to disable copy and paste

$('input,textarea').bind('cut copy paste', function (e) {
    e.preventDefault(); //disable cut,copy,paste
});


来源:https://stackoverflow.com/questions/22773049/disable-text-selection-on-input-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!