Я создаю живой поиск ajax, использую функцию заполнения. поэтому, когда я щелкнул слово, которое всплывает при поиске, оно переходит в другую форму ввода. но я хочу, чтобы он при заполнении не заменял старое слово.
например у меня есть input1 и input2. input1 предназначен для поиска, а input2 - для ввода слова, которое я выбрал. например, я уже выбрал word1 из input1. word1 заполнит input2.
вот так input2 = word1
но когда я ищу другое слово в input1, я хочу, чтобы это слово не заменяло input2, а добавляло его. например, я выбираю word2, поэтому в input2 будет
как этот input2 = word1, word2
вот мой живой скрипт поиска
function fill(Value) {
$('#tag_list').val(Value);
$('#display').hide();
}
$(document).ready(function() {
$("#tag").keyup(function() {
$('#display').show();
var name = $('#tag').val();
if (name == "") {
$("#display").html("");
$("#display").hide();
}
else {
$.ajax({
type: "POST",
url: "../cari/tag.php",
data: {
search: name
},
success: function(html) {
$("#display").html(html).show();
}
});
}
});
});
мой код tag.php
<div class = "result" onclick='fill("<?php echo $caris['id_t']; ?>,")'>
tag.php с onclick
просто удалите старое значение с помощью '$ (' # tag_list '). val ("");' а затем присвоить новое значение



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы можете добавить значения, как показано ниже
function fill(Value) {
var $tagList = $('#tag_list');
var previousVal = $tagList.val();
if (previousVal) {
previousVal += ", ";
}
$tagList.val(previousVal + Value);
$('#display').hide();
}
он заполняется вот так, 2, 3
@Jazuly чего ты хочешь? без запятой?
вот так 2, 3 не , 2, 3
Вы можете использовать Дополнительное задание:
The addition assignment operator adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible. See the addition operator for more details.
Синтаксис
Operator: x += y
Meaning: x = x + y
для вашего случая:
var query = '';
$('#search').on('click',function(){
fill();
});
function fill() {
query += $('#input1').val() + ',';
$('#input2').val(query.slice(0,-1));
/* The slice function will remove the last caractere from string ',' */
}<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type = "text" name = "input1" id = "input1">
<input type = "text" name = "input2" id = "input2">
<button id = "search">search</button>
откуда вы вызываете функцию
fill?