jQuery - Append a Value to a INPUT, keeping it a Comma Delimited list

后端 未结 4 848
Happy的楠姐
Happy的楠姐 2020-12-16 14:42

I have an input like follows:


I\'d like to be able to append a value to th

相关标签:
4条回答
  • 2020-12-16 14:52

    Just add a conditional when you add the value to the field.

    var cur_val = $('#attachment-uuids').val();
    if(cur_val)
      $('#attachment-uuids').val(cur_val + "," + new_val);
    else
      $('#attachment-uuids').val(new_val);
    
    0 讨论(0)
  • 2020-12-16 14:55

    I believe the .append() function is exactly for this purpose. I just tried:

    $("#attachment-uuids").append(new_val); 
    

    to append values to a field. This works and "attachment-uuids" is populated with CSVs, as required.

    0 讨论(0)
  • 2020-12-16 15:13

    Append won't work for input. But you can use:

    $("#txt1").get(0).value+="+";
    
    0 讨论(0)
  • 2020-12-16 15:15
    $('#attachment-uuids').val(function(i,val) { 
         return val + (!val ? '' : ', ') + '66666';
    });
    

    EDIT: As @mkoryak noted, I'm doing an unnecessary negation of val in the conditional operator. It could be rewritten without the ! as:

    (val ? ', ' : '')
    
    0 讨论(0)
提交回复
热议问题