<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 20:08:03 浏览: 20
可以在 HTML 中给 textarea 元素设置一个 `hidden` 属性,使其一开始不可见。然后在点击打开文件按钮时,使用 jQuery 的 `show()` 方法将其显示出来。修改后的代码如下:
```html
<h1>编辑NTP配置文件</h1>
<hr>
<button id="openBtn">打开文件</button>
<button id="saveBtn">保存</button>
<br><br>
<textarea id="fileContent" cols="80" rows="30" hidden></textarea>
<script>
$(document).ready(function() {
$("#openBtn").click(function() {
$.ajax({
url: "ntp1.php",
method: "POST",
dataType: "text",
success: function(data) {
$("#fileContent").val(data);
$("#fileContent").show();
}
});
});
$("#saveBtn").click(function() {
var fileContent = $("#fileContent").val();
$.ajax({
url: "ntp1.php",
method: "POST",
data: { content: fileContent },
success: function(response) {
alert("File saved successfully!");
}
});
});
});
</script>
```