JS禁止input表单元素手动输入方法(转)

第二种方法 本人测试可用

1. 替代法

使用隐藏的<input type="file"/>控件,然后用一个只读的文本框和一个按钮来模拟<input type="file"/>的功能。

<input type="file" name="file" onpropertychange="file1.value=this.value" style=display:none/>
<input type="text" name="file1" readonly/>
<input type="button" value="浏览" id="button1" name="button1" onclick="file.click()"/>

2. 使用脚本事件限制控件输入

将<input type="file"/>控件的鼠标右键菜单、按键事件限制住,不让用户有机会输入。 www.it165.net

<input type="file" onkeydown="return false" onkeyup="return false" oncontextmenu="return false">

3. 使用contenteditable属性

使用该属性可以有效地限制用户在<input type="file"/>控件中手动输入内容,而只能通过文件选择对话框选择文件。

<input type="file" id="file1" contenteditable="false" />

原文地址:https://www.cnblogs.com/xiabaizhu/p/4109837.html