清空按钮后回答按钮失效的解决方法 - JavaScript 代码优化
<body>
<script src="js/Cg.js"></script>
<div class="basic-portfolio-area ptb-10">
<div class="filter-menu text-center">
</div>
<div class="call-to-action-area gray-bg ptb-60">
<div class="container">
<div class="form-group">
<label></label>
<textarea class="form-control" id="chat-gpt-input" placeholder="输入问题" rows="3" resize="none" style="width: 100%; margin: 0 auto; background-color: #f4f4f4; color: #333; border: 1px solid #ccc; border-radius: 12px;"></textarea>
</div>
<div class="call-to-action">
<a onclick="callCHATGPT()" class="btn btn-large" href="#" id="call-to-action-btn" style="background-color: #4cae4c; color: #fff; font-weight: bold; border-radius: 5px; padding: 10px 20px; border: none; box-shadow: none; text-decoration:none;" onmousedown="this.style.backgroundColor='#3c903c'" onmouseup="this.style.backgroundColor='#4cae4c'">回答</a>
<a onclick="clearTextArea()" class="btn btn-large" href="#" style="background-color: #d9534f; color: #fff; font-weight: bold; border-radius: 5px; padding: 10px 20px; border: none; box-shadow: none; text-decoration:none; margin-left: 20px;" onmousedown="this.style.backgroundColor='#c9302c'" onmouseup="this.style.backgroundColor='#d9534f'">清空</a>
</div>
<div class="form-group">
<label></label>
<textarea class="form-control" id="chatgpt-response" placeholder="长途访问,请耐心等待回答 Ai生成它很快,但是由于网络问题我们需要等待,通常内容越长等待越久 如果长时间没反应请刷新页面重试" rows="26" resize="none" style="width: 100%;height: auto; margin: 0 auto; background-color: #f4f4f4; color: #333; border: 1px solid #ccc; border-radius: 10px; overflow: scroll;"></textarea>
</div>
</div>
<footer>
<div class="basic-footer gray-bg text-center ptb-20">
<div class="container">
<div class="footer-menu mt-30">
<p><a href="untitled.html">如果不懂怎么发布命令请进入</a></p>
<nav>
<ul>
<li><a href="https://wy2023.icu" target="_blank">云客商城</a></li>
<li><a href="https://ai.wy2023.icu/ai-1" target="_blank">返回AI-1</a></li>
<li><a href="https://ai.wy2023.icu" target="_blank">返回首页</a></li>
</ul>
</nav>
</div>
</div>
</div>
</footer>
</div>
</div>
<script>
function clearTextArea() {
document.getElementById("chat-gpt-input").value = "";
document.getElementById("chatgpt-response").value = "";
document.getElementById("call-to-action-btn").disabled = false;
// 添加这一行代码使回答按钮重新可用
}
<pre><code>function callCHATGPT() {
var input = document.getElementById("chat-gpt-input").value.trim();
if (input == "") {
alert("请输入问题");
return;
}
document.getElementById("chatgpt-response").value = "正在思考,请稍等...";
document.getElementById("call-to-action-btn").disabled = true; // 添加这一行代码使回答按钮不可用
var url = "https://api.openai.com/v1/engines/davinci-codex/completions";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer " + API_KEY);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
var answer = response.choices[0].text.trim();
document.getElementById("chatgpt-response").value = answer;
document.getElementById("call-to-action-btn").disabled = false; // 添加这一行代码使回答按钮重新可用
}
};
var data = JSON.stringify({
prompt: input,
max_tokens: 1024,
temperature: 0.5,
n: 1,
stop: "\n",
});
xhr.send(data);
}
</code></pre>
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/mmW5 著作权归作者所有。请勿转载和采集!