附加文本到input字段

我需要附加一些文本到input字段…

$('#input-field-id').val($('#input-field-id').val() + 'more text'); 

有两个选项。 艾曼的方法是最简单的,但我会添加一个额外的笔记。 你真的应该cachingjQueryselect,没有理由两次调用$("#input-field-id")

 var input = $( "#input-field-id" ); input.val( input.val() + "more text" ); 

另一个选项.val()也可以把一个函数作为参数。 这具有在多个input上轻松工作的优点:

 $( "input" ).val( function( index, val ) { return val + "more text"; }); 

如果您计划使用多次追加,则可能需要编写一个函数:

 //Append text to input element function jQ_append(id_of_input, text){ var input_id = '#'+id_of_input; $(input_id).val($(input_id).val() + text); } 

你可以直接调用它:

 jQ_append('my_input_id', 'add this text'); 

你可能正在寻找val()

  // Define appendVal by extending JQuery $.fn.appendVal = function( TextToAppend ) { return $(this).val( $(this).val() + TextToAppend ); }; //_____________________________________________ // And that's how to use it: $('#SomeID') .appendVal( 'This text was just added' ) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <textarea id = "SomeID" value = "ValueText" type = "text" >Current NodeText </textarea> </form> 
 <!DOCTYPE html> <html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <style type="text/css"> *{ font-family: arial; font-size: 15px; } </style> </head> <body> <button id="more">More</button><br/><br/> <div> User Name : <input type="text" class="users"/><br/><br/> </div> <button id="btn_data">Send Data</button> <script type="text/javascript"> jQuery(document).ready(function($) { $('#more').on('click',function(x){ var textMore = "User Name : <input type='text' class='users'/><br/><br/>"; $("div").append(textMore); }); $('#btn_data').on('click',function(x){ var users=$(".users"); $(users).each(function(i, e) { console.log($(e).val()); }); }) }); </script> </body> </html> 

产量 在这里输入图像说明