简体   繁体   English

this.select()和表单上的javascript事件

[英]this.select() and javascript events on forms

I have a form box that I want to be always selected. 我有一个始终希望被选中的表单框。 I was able to get it to select everything in the box when it is clicked (using onFocus="this.select()") but I want it to be selected 100% of the time. 我可以让它在单击时选择框中的所有内容(使用onFocus =“ this.select()”),但我希望100%的时间选择它。 The text in the box will be always changing, so I tried using onChange="this.select()" but that didn't work. 框中的文本将始终处于更改状态,因此我尝试使用onChange =“ this.select()”,但此操作无效。 Here's what I have: 这是我所拥有的:

    <form>
    <input type="text" id="txt1" size="30" maxlength="1" 
onkeyup="showHint(this.value)" onFocus="this.select()" onBlur="this.select()" 
onChange="this.select()" value="Click here, then press a key"/>
    </form>

Basically I just tried to call everything in hopes that something would work, but it is still acting as if only onFocus="this.select()" is there. 基本上,我只是尝试调用所有东西,以期希望某些东西能起作用,但是它仍然表现得好像只有onFocus =“ this.select()”在那里。 By the way, this is for controlling something via keyboard, which is why the maxlength is only 1. I want it to be always selected so that when new key are pressed, the last command will be changed without having to use backspace. 顺便说一句,这是为了通过键盘控制某些东西,这就是为什么maxlength只有1的原因。我希望始终选择它,以便在按下新键时可以更改最后一个命令而不必使用退格键。

Is there a reason you aren't just using document keystroke detection? 您不仅仅使用文档击键检测是有原因的吗? If you need the value to appear in the input field for some reason, once you detect the keystroke, you could fill the text box. 如果出于某种原因您需要将该值显示在输入字段中,则一旦检测到击键,就可以填充文本框。 This would be much simpler than trying to maintain focus on the field itself. 这比尝试只专注于该领域本身要简单得多。

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

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