add to value of input instead of overwrite val
This is my code
$('#enterTags').keydown(function(event) {
var tag = $(this).val();
// 188 is comma, 13 is enter
if (event.w开发者_StackOverflow中文版hich === 188 || event.which === 13) {
if (tag) {
$('#tags').append('<div>' + tag + '</div>');
**$('#falseInput').val(tag + ', ');**
$(this).val('');
}
event.preventDefault();
}
// 8 is backspace
else if (event.which == 8) {
if (!tag) {
$('#tags div:last').remove();
event.preventDefault();
}
}
});
The section of code I bolded is what I am having problems with. I need the value of #enterTags to be added to that of #falseInput, not overwrite it. How can I do this?
Try:
$('#falseInput').val($('#falseInput').val() + tag + ', ');
or:
$('#falseInput').val(function(index, value) {
return value + tag + ', ';
});
Try something like this:
$('#falseInput').val($('#falseInput').val() + ', ' + tag);
精彩评论