gpt4 book ai didi

javascript - 输入元素的值发生变化但未显示在浏览器中?

转载 作者:行者123 更新时间:2023-11-30 14:16:39 26 4
gpt4 key购买 nike

我为表单的一部分编写了一个实时搜索字段,当出现建议并且用户点击其中一个时,一个 js 函数 (selectedService) 正在运行调用获取用户点击的liinnerHTML!当您在浏览器的“检查元素”中查看代码时一切正常,但在网页上却不行!

用户点击建议后,输入元素的值属性在代码中发生变化,但在浏览器中发生变化。输入字段的内容仍然是用户在其中输入的内容。

代码如下:

<!DOCTYPE html>
<html dir="rtl">
<head>
<meta charset="UTF-8">
</head>
<body>

<input value="" id="services" placeholder="click or type plz..."/>
<ul id="suggestions">
</ul>
<style>
/* some styles */
</style>
<script>
var input = document.getElementById("services");
var suggestions = document.getElementById("suggestions");

input.oninput = function() {
var q = input.value;

if (q.length < 2) {
suggestions.setAttribute("style","display: none");
return;
}
var xhr2 = new XMLHttpRequest;
xhr2.open("GET", "theURL/service-request-handler.php?q="+q, true);
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4 && xhr2.status == 200)
{
var result = xhr2.responseText;
console.log("server response = "+result);
var json = JSON.parse(result);
showSuggestions(json);
}
};
xhr2.send();
};

function showSuggestions(json) {
suggestions.setAttribute("style","display: block");
var li_list = '';
var i = 0;
for (i=0; i<json.length; i++)
li_list += '<li id="'+i+'" onclick="selectedService('+i+')">'+json[i]+'</li>';

suggestions.innerHTML = li_list;
}

function selectedService(i) {
li_target = document.getElementById(i);
console.log(li_target.innerHTML);
// input.innerHTML = li_target.innerHTML;
input.setAttribute("value",li_target.innerHTML);
suggestions.setAttribute("style","display: none");
}


</script>

</body>
</html>

检查元素中的结果: before onclick event

when event happens!

如果有任何帮助或建议,我将不胜感激:)

最佳答案

input.value = li_target.innerHTML

关于javascript - 输入元素的值发生变化但未显示在浏览器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53476346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com