开发者

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

I have an input like follows:

<input type="hidden" id="attachment-uuids" value="">

I'd like to be able to append a value to the Input, at different times:

$('#attachment-uuids).val('55555');

results in:

<input type="hidden" id="attachment-uuids" value="55555">

But then doing:

$('#attachment-uuids).val('66666');

results in:

<input type="h开发者_如何转开发idden" id="attachment-uuids" value="66666">

Where I'd like the following:

<input type="hidden" id="attachment-uuids" value="55555, 66666">

how can I append values when the value is empty and when the value is not empty with a comma delimited list?

Thanks


$('#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 ? ', ' : '')


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);


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

$("#txt1").get(0).value+="+";


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜