Javascript 按此键时删除input.value
我的页面上有一个HTML输入。用户可以在其中键入文本。当他输入我指定的命令并按enter键时,页面将信息输出到Javascript 按此键时删除input.value,javascript,Javascript,我的页面上有一个HTML输入。用户可以在其中键入文本。当他输入我指定的命令并按enter键时,页面将信息输出到input.value。如果用户输入一些随机的内容并确认其输入,页面只会输出:“未知命令”,再次输入到toinput.value 我在这里做了一把带条纹的小提琴: 问题: 当我输入:test并按enter键时,该值变为:这是一种工作状态…。我知道我想输入一些新的内容,但我首先必须突出显示或删除这是一种工作方式…文本,这确实不直观 有没有办法改变我的脚本,这样当我在输入时,我按下任何按钮,
input.value
。如果用户输入一些随机的内容并确认其输入,页面只会输出:“未知命令”,再次输入到toinput.value
我在这里做了一把带条纹的小提琴:
问题:当我输入:
test
并按enter键时,该值变为:这是一种工作状态…
。我知道我想输入一些新的内容,但我首先必须突出显示或删除这是一种工作方式…
文本,这确实不直观
有没有办法改变我的脚本,这样当我在输入时,我按下任何按钮,即不是按钮Nr.13又称“Enter”,页面只会生成输入值,即按下的按钮?这样用户在收到一个值后就可以开始输入新的内容,而不必删除我输入的值
我尝试添加一个额外的.onkeypress函数,但它破坏了一切,所以我没有正确的方法
这是我现在的JS:
var clInput = 0;
document.querySelector("#inputMain").onkeypress = function(e){
if (!e) e = window.event;
if (e.keyCode == '13'){
clInput = document.querySelector("#inputMain").value;
switch (clInput) {
case "test":
test();
break;
default:
document.querySelector("#inputMain").value = "Unknown command.";
}
return false;
}
}
function test() {
document.querySelector("#inputMain").value = "This is kind of working…";
}
HTML:
如果我不明白你想说什么,很抱歉 但是,如果您希望用户在收到值后就可以开始键入新的内容,而不必删除您在其中输入的值。 你能行
function test() {
document.querySelector("#inputMain").value = "This is kind of working…";
document.querySelector("#inputMain").selct();
}
这将选择输入字段的所有文本,当用户键入某个内容时,该字段以前的值将被删除我已稍微更新了您的代码,以完全满足您的需要。所做的基本工作是:
13-输入
13-Enter
,只需确保清除输入即可只需在输入上设置
占位符
,并清空值。我已经得到了一个有效的答案,但谢谢!很高兴它有帮助!
function test() {
document.querySelector("#inputMain").value = "This is kind of working…";
document.querySelector("#inputMain").selct();
}
var clInput = 0; // Note: Ignore Upper- / Lower-Case in input?
var isEntered = false;
document.querySelector("#inputMain").onkeypress = function(e){
if (!e) e = window.event;
// clear the value
if (isEntered) {
isEntered = false;
document.querySelector("#inputMain").value = '';
}
if (e.keyCode == '13'){
clInput = document.querySelector("#inputMain").value;
isEntered = true;
switch (clInput) {
case "test":
test();
break;
default:
document.querySelector("#inputMain").value = "Unknown command.";
}
return false;
}
}
function test() {
document.querySelector("#inputMain").value = "This is kind of working…";
}