在这种情况下,我使用jquery和ajax提交值后如何保留值?

Click http://jsfiddle.net/4y1b1j8g/13/ to see the result. When I input a value in the input and press Enter, the value will replace dog value. But that is not what I want. I want to keep the value. for example, enter cat, it will become dog cat, enter mouse, it will like dog cat mouse. How can I do that?Appreciate.

$( "#input" ).keydown(function( event ) {

if ( event.which == 13 ) {
   event.preventDefault();
    
    //put input value into div
var value=$('#input').val();
    $('#test').text(value);
   
   
     
}
    
    
});
<div class="test" id="test">dog</div>
<input type="text" id="input"/>

</div>

What you want to do is concatenate the string, not replace it. You'll need to get the previous value (.html()) and add the new value to it.

You could do that simply, by changing your .text() line to this:

$('#test').text($('#test').html() + " " + value);

As you can see, we grab the previous value from the div via the .html() event and then append the value.

Example


You could alternatively use the .append() method which will concatenate the string for you:

$('#test').append(" " + value);

And here is an example for this method: JSFiddle Demo


Notes

The " " + value is adding a white space before you append the string, you can simple remove it if you don't want that, I just assumed you wanted readable text.

use append. Try this:

 $("#input").keydown(function(event) {
        if (event.which == 13) {
            event.preventDefault();

            //put input value into div
            var value = $('#input').val();
            $('#test').append(value);
        }
    });