Append text to input field

后端 未结 6 1167
广开言路
广开言路 2020-11-28 05:38

I need to append some text to an input field...

6条回答
  •  醉话见心
    2020-11-28 05:50

    There are two options. Ayman's approach is the most simple, but I would add one extra note to it. You should really cache jQuery selections, there is no reason to call $("#input-field-id") twice:

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

    The other option, .val() can also take a function as an argument. This has the advantange of easily working on multiple inputs:

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

提交回复
热议问题