简体   繁体   English

按下“输入”键时删除输入框中的文本

[英]Delete text in an input box when 'enter' key is pressed

I want to create a google search that when the user presses the 'enter' key, it will search and then delete the text in the input field. 我想创建一个Google搜索,当用户按下“输入”键时,它将搜索然后删除输入字段中的文本。 My search opens the search in a new tab. 我的搜索在新选项卡中打开搜索。

My java code that tries to do that: 我的Java代码试图做到这一点:

final JTextArea ta = new JTextArea();
ta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            ta.setText("");
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }
});
}

My Google Search Input Field Code: 我的Google搜索输入字段代码:

<input class="searchbar1" type="Search" dir="ltr" onload="self.focus();" autofocus required align="center" id="GoogleSearch" value="" name="q" placeholder="" autocomplete="off">

You need to call a jscript function which like this.. call this in your submit button onclick.. 您需要调用一个像这样的jscript函数。在onclick的提交按钮中调用它。

function cleartext()
{
document.getElementById(GoogleSearch).text="";
}

Use javascript's innerHTML to replace the filled input field with the same input field but empty. 使用javascript的innerHTML将填充的输入字段替换为相同的输入字段,但为空。 ( <input class="searchbar1" type="Search" dir="ltr" onload="self.focus();" autofocus required align="center" id="GoogleSearch" value="" name="q" placeholder="" autocomplete="off"> ) <input class="searchbar1" type="Search" dir="ltr" onload="self.focus();" autofocus required align="center" id="GoogleSearch" value="" name="q" placeholder="" autocomplete="off">

Your question seems unclear, but I would recommend adding an onchange handler to the field like this: 您的问题似乎不清楚,但是我建议向这样的字段添加一个onchange处理程序:

document.getElementById('GoogleSearch').addEventListener('change',function(){
    //somehow search for the term here
    //remove text
    document.getElementById('GoogleSearch').value='';
},false);

Note that this actually will also do the search when the user clicks out of the field, now just when they hit enter. 请注意,实际上,这也将在用户单击字段外时(现在仅是按回车键)进行搜索。 If you want exclusively enter button, tell me in the comments 如果您只想输入按钮,请在评论中告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM