简体   繁体   中英

Autofill input with another input values

I need to autofill an input field with the values of another input fields. So far I have this:

$("#field1, #field2").keyup(function(){
    $("#result").val(this.value);
});

<input type="text" id="field1" name="field1" value="" >
<input type="text" id="field2" name="field2" value="" >

//* to be filled with the values of the inputs above *// 
<input type="text" id="result" name="resut" value="">

What about

$("#field1, #field2").keyup(function(){
    update();
});

function update() {
  $("#result").val($('#field1').val() + " " + $('#field2').val());
}

 $("#field1, #field2").keyup(function(){ update(); }); function update() { $("#result").val($('#field1').val() + " " + $('#field2').val()); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="field1" name="field1" value="" > <input type="text" id="field2" name="field2" value="" > <br> <input type="text" id="result" name="resut" value=""> 

 $("#field1, #field2").keyup(function(){ update(); }); function update() { $("#result").val($('#field1').val() + " " + $('#field2').val()); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="field1" name="field1" value="" > <input type="text" id="field2" name="field2" value="" > <br> <input type="text" id="result" name="resut" value=""> 

 $("#field1, #field2").keyup(function(){ update(); }); function update() { var a = $('#field1').val(); var b = $('#field2').val(); $("#result").val('x'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="field1" name="field1" value="" > <input type="text" id="field2" name="field2" value="" > <br> <input type="text" id="result" name="resut" value=""> 

Solutions provided are excellent and they work. However, I'm wondering if it is possible to truncate number of characters from any of the input fields?

Im trying to generate username out of input on lastname, firstname, middlename. Example: lastname firstname middlename Username generated should be lastnamefm?

I dont know how to truncate all except first character of the fields firstname and middlename.

Thanks in advance!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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