doukangbin9698 2015-02-17 01:05
浏览 21
已采纳

在这种情况下,我使用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>
  • 写回答

2条回答 默认 最新

  • drflkphi675447 2015-02-17 01:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题