简体   繁体   English

使用javascript自动在onfocus的输入文本字段中附加值

[英]Automatically Append a value in an Input Text field onfocus using javascript

In a create form, the user types his name, then his surname, and when he focuses on the username text field, the surname.name String must be appended automatically. 在创建表单中,用户键入他的姓名,然后输入他的姓氏,当他关注用户名文本字段时,必须自动附加surname.name字符串。 In other words, Using javascript, once the USERNAME input text field receives focus, the values of NAME & SURNAME input text fields must be appended to it automatically and separated by a dot (.) = surname.name 换句话说,使用javascript,一旦USERNAME输入文本字段获得焦点,NAME和SURNAME输入文本字段的值必须自动附加到它上并用点(。)= surname.name分隔

     <h:outputLabel value="#{bundle.CreateUsersLabel_name}" for="name" />
                    <h:inputText id="name" value="#{usersController.selected.name}" title="#{bundle.CreateUsersTitle_name}" />
                    <h:outputLabel value="#{bundle.CreateUsersLabel_surname}" for="surname" />
                    <h:inputText id="surname" value="#{usersController.selected.surname}" title="#{bundle.CreateUsersTitle_surname}" />
 <h:outputLabel value="#{bundle.CreateUsersLabel_username}" for="username" />
                    <h:inputText id="username" value="#{usersController.selected.username}" title="#{bundle.CreateUsersTitle_username}" required="true" requiredMessage="#{bundle.CreateUsersRequiredMessage_username}"/>

Any ideas please? 有什么想法吗?

Here is a small demo that I made hopefully this is what you are looking for 这是一个小型的演示,我希望这是你正在寻找的

name:    <input id="name" /> <br>
surname: <input id="surname" /><br>
username: <input onfocus="input()" id="username" />

those are the inputs and here is the function that gets called onfocus in the username field 这些是输入,这里是在用户名字段中调用onfocus的函数

function input(){
var name = document.getElementById('name').value;
var surname = document.getElementById('surname').value;
var username = '';

//only fill in the field if name and surname are entered 
if(name != '' && surname != ''){
    username = name+'.'+surname;
    document.getElementById('username').value = username;
}
}

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

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