HTML <输入类型=“文本”... as <输入类型=“文件”

发布于 2024-10-20 12:16:56 字数 179 浏览 2 评论 0 原文

如何将 设置为只读但允许删除其值?

换句话说,如何实现像 这样的行为,您无法手动输入文件路径,但可以删除通过“浏览”按钮注入的内容。

How to set <input type="text"... to be read-only but to allow deletion of its value?

In other words how to achieve behavior like <input type="file"..., where you cannot manually enter path to file but you can delete what has been injected by "Browse" button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回忆那么伤 2024-10-27 12:16:56

jquery:

<input type="text" id="name1" name="name" readonly="readonly" value="demovalue" />
<input type="button" onclick="$('#name1').val('');" value="clear">

基本:

<input type="text" id="name1" name="name" readonly="readonly" value="demovalue" />
<input type="button" onclick="document.getElementById('name1').value='';" value="clear">

jquery:

<input type="text" id="name1" name="name" readonly="readonly" value="demovalue" />
<input type="button" onclick="$('#name1').val('');" value="clear">

basic:

<input type="text" id="name1" name="name" readonly="readonly" value="demovalue" />
<input type="button" onclick="document.getElementById('name1').value='';" value="clear">
兔小萌 2024-10-27 12:16:56

考虑此 HTML:

<input type="text" class="onlydelete" value="Nyedva Nyedva" />

以下 jQuery 函数将仅允许在类 onlydelete 的输入字段中使用 Backspace 键。

$('.onlydelete').keypress(function (e) {
    return (e.which===8);
});

更新:

我发现您还需要删除键。我想您还希望允许箭头键让用户移动插入符号。对于这些特殊键,您可以使用 keydown。以下代码片段仅允许删除 (46)、退格键 (8) 和箭头键 (37-40)。

$('.onlydelete').keydown(function (e) {
    return (e.which===46 || e.which===8 || (e.which>=37 && e.which<=40));
});

更新 2:

添加类的另一个好处是您可以使用 css 轻松设置这些特殊输入的样式。例如:

.onlydelete { background-color: #aaaaaa; }

Considering this HTML:

<input type="text" class="onlydelete" value="Nyedva Nyedva" />

The following jQuery function will only allow the Backspace key to be used in the input fields with class onlydelete.

$('.onlydelete').keypress(function (e) {
    return (e.which===8);
});

UPDATE:

I've found that you also need the Delete key. And I guess you would also like to allow the arrow keys to let the user move the caret. For these special keys, you can use keydown. The following snippet only allows Delete (46), Backspace (8), and arrow keys (37-40).

$('.onlydelete').keydown(function (e) {
    return (e.which===46 || e.which===8 || (e.which>=37 && e.which<=40));
});

UPDATE 2:

The other good thing about adding a class is that you can easily style these special inputs with css. For example:

.onlydelete { background-color: #aaaaaa; }
扛刀软妹 2024-10-27 12:16:56

像这样的东西?

<input type="text" onclick="this.value='',this.disabled='disabled'" value="text">

如果您不想将背景更改为灰色,您可以添加以下 css:

input[disabled] {background:white;border:1px solid #7F9DB9}

演示: http://jsfiddle.net/utw8y/1

更新

仅使用删除或退格键,您可以使用以下 jQuery 代码:

$('input').keypress(function(event) {
    if ((event.which!='0') && (event.which!='8')) {
     event.preventDefault();
    } else {
    $(this).val("");    
    }
});

演示: < a href="http://jsfiddle.net/utw8y/2/" rel="nofollow">http://jsfiddle.net/utw8y/2/

something like this?

<input type="text" onclick="this.value='',this.disabled='disabled'" value="text">

if you don't want to change the background to grey, you can add and the following css:

input[disabled] {background:white;border:1px solid #7F9DB9}

Demo: http://jsfiddle.net/utw8y/1

Update

Using only delete or backspace keys you can use the following jQuery code:

$('input').keypress(function(event) {
    if ((event.which!='0') && (event.which!='8')) {
     event.preventDefault();
    } else {
    $(this).val("");    
    }
});

Demo: http://jsfiddle.net/utw8y/2/

她比我温柔 2024-10-27 12:16:56

试试这个:

var inputBox = document.getElementById("inputBox");

inputBox.onkeypress = checkInput;

function checkInput(e) {
  // checking if the pressed key is delete or backspace
  // if not, we prevent character input
  if (e.keyCode != 8 || e.keyCode != 46) {
    e.preventDefault();
  }
}

// also a button to clear the input value
document.getElementById("delete").onclick = function() {
  inputBox.value = "";
}
<input type="text" id="inputBox" value="this is a test" />
<input type="button" id="delete" value="delete" />

Try this:

var inputBox = document.getElementById("inputBox");

inputBox.onkeypress = checkInput;

function checkInput(e) {
  // checking if the pressed key is delete or backspace
  // if not, we prevent character input
  if (e.keyCode != 8 || e.keyCode != 46) {
    e.preventDefault();
  }
}

// also a button to clear the input value
document.getElementById("delete").onclick = function() {
  inputBox.value = "";
}
<input type="text" id="inputBox" value="this is a test" />
<input type="button" id="delete" value="delete" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文