为什么,点击
span
,它的文本不会被抛出到
input
?
html格式
<script>
$(document).ready(function () {
$('#ajax-service').keyup(function () {
$.ajax({
data: $(this).serialize(),
url: "{% url 'ajax_request' %}",
success: function (response) {
if (response.is_exist == true) {
let list = ''
for (var i in response.is_taken) {
list = list + '<span name="'+(response.is_taken[i])+'" id="#service'+(i)+'">'+(response.is_taken[i])+'</span> '
}
let elem_input = document.querySelector("#ajax-service")
let elem = document.querySelector("#service"+i);
$('#ajax-service-list').remove();
$('#ajax-service').after('<div id="ajax-service-list" name="ajax-service-list">'+(list)+'</div>')
elem.addEventListener("click", function(e){
elem_input.value = e.target.getAttribute("name");
});
}
else {
$('#ajax-service-list').remove();
$('#ajax-service').after('<div id="ajax-service-list" name="ajax-service-list"><span class="badge rounded-pill bg-warning text-dark" id="ajax-service-list">ÐÐµÑ Ñовпадений</span></div>')
}
},
error: function (response) {
console.log(response.responseJSON.errors)
}
});
return false;
});
})
</script>
?category=simple:151 Uncaught TypeError: Cannot read property 'addEventListener' of null
at Object.success (?category=simple:151)
at c (jquery-3.6.0.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.6.0.min.js:2)
at l (jquery-3.6.0.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.6.0.min.js:2)