<h1>编辑NTP配置文件</h1> <hr> <button id = "openBtn">打开文件</button> <button id = "saveBtn">保存</button> <br><br> <textarea id="fileContent" cols="80" rows="30"></textarea> <script> $(document).ready(function(){ $("#openBtn").click(function(){ $.ajax({ url:"ntp1.php", method:"POST", dataType:"text", success:function(data){ $("#fileContent").val(data); } }); }); $("#saveBtn").click(function(){ var fileContent = $("#fileContent").val(); $.ajax({ url:"ntp1.php", method:"POST", data:{content:fileContent}, success:function(response){ alert("File saved successfully!"); } }); }); }); </script> 让此代码的文本框只有在点击打开文件的按钮时才显示 点击保存的按钮时隐藏
时间: 2024-02-10 11:08:03 浏览: 25
好的,您可以在代码中加入以下JavaScript代码实现这个功能:
```
$(document).ready(function(){
$("#saveBtn").hide(); // 隐藏保存按钮
$("#fileContent").hide(); // 隐藏文本框
$("#openBtn").click(function(){
$.ajax({
url:"ntp1.php",
method:"POST",
dataType:"text",
success:function(data){
$("#fileContent").val(data);
$("#fileContent").show(); // 显示文本框
$("#saveBtn").show(); // 显示保存按钮
}
});
});
$("#saveBtn").click(function(){
var fileContent = $("#fileContent").val();
$.ajax({
url:"ntp1.php",
method:"POST",
data:{content:fileContent},
success:function(response){
alert("File saved successfully!");
$("#fileContent").hide(); // 隐藏文本框
$("#saveBtn").hide(); // 隐藏保存按钮
}
});
});
});
```
这段代码在页面加载完成后,先将保存按钮和文本框隐藏起来。当打开文件按钮被点击时,发送AJAX请求获取文件内容,并将文本框和保存按钮显示出来。当保存按钮被点击时,发送AJAX请求保存文件,并将文本框和保存按钮隐藏起来。