在编辑文本后追加功能对textarea不起作用

时间:2015-04-07 10:17:45

标签: javascript jquery

当textarea文本编辑时,附加功能不会附加。我试图拉入当前值,追加并放回去。但无法工作。

function getSerial() { 
  var getValue = $(serial).val() + ",\n"; 
  var showValue = "#showSerial";
  $(showValue).append(getValue);
} 

如果我使用val(),它将在提交的值之前替换。每次提交时,文本区域的值将添加逗号,如AL-2,Al-3。

5 个答案:

答案 0 :(得分:2)

代替.append()使用.val()

答案 1 :(得分:1)

而不是.append()使用.val(),如下所示: -

function getSerial (){ 
   var getValue = $(serial).val()+ ",\n"; //make sure serial variable is defined above this line
   var showValue = "#showSerial";
   $(showValue).val(getValue + $(showValue).text()); //instead of .append() use .val()
}

答案 2 :(得分:1)

您的ID元素是textarea。您应该使用.val()代替.append()

如何:

function getSerial() { 
  var getValue = $(serial).val() + ",\n"; 
  $("#showSerial").val(getValue);
} 

答案 3 :(得分:1)

function getSerial (){ 
   var getValue = $(serial).val()+ ",\n";
   var showValue = "#showSerial";
   $(showValue).val(getValue);
}

答案 4 :(得分:0)

尝试使用val()代替append():我已经创建了一个测试小提琴,这对你有用吗?

function getSerial (){ 
    var showValue = $("#showSerial");
    var getValue   =  $(serial).val()+ ",\n"; 
    if(showValue.val()){
        $(showValue).val(showValue.val() + getValue);
    }else {
        $(showValue).val(getValue);
    }
}

https://jsfiddle.net/mc5rrkmu/1/

相关问题