gpt4 book ai didi

jquery - 使用 jQuery 在网页中模仿 CLI

转载 作者:行者123 更新时间:2023-12-01 06:07:48 24 4
gpt4 key购买 nike

我想模仿 CLI在网页中,用户在页面上键入命令,该命令会发送到服务器执行,响应会显示在页面中 - 非常类似于 CLI 行为。

我的 jQuery fu 不是它应该的样子,我认为这一定是一种必须经常出现的“模式”,所以也许有人可以指向具有类似代码的资源,或者也许有人可以让我开始此处发布了一个片段。

这是我的第一次尝试。欢迎更正、改进。

<html>
<head>
<title>Simple CLI</title>
<script src="jquery.js">
</head>
<body>
<div>
<div id="console">
</div>
<!-- Is having an input tag without an enclosing form tag valid (X)HTML? -->
<input id="cmd_input" type="text" name="cli_cmd" />
<div
<script type="text/javascript">
/* [CDATA[ */
$(document).ready(function(){
$.post('url': "example.php", {'cmd': $('#cmd_input').val()},
function(data){ $('#cmd_input').append(data.resp); },
'type': 'json');
});
/* ]]> */
</script>
</body>
</html>

最佳答案

这应该是对 <enter> 的 ajax 请求

<html>
<head>
<title>Simple CLI</title>
<script src="jquery.js">
</head>
<body>
<div>
<div id="out"></div>
<input id="in" type="text" />
</div>
<script type="text/javascript">
$('#in').keyup(function(e) {
if (e.which !== 13)
return;
var cmd = { cmd: $(this).val() };
$(this).val('');
$.post('example.php', cmd, function(data) {
$('#out').append(data);
});
};
</script>
</body>
</html>

关于jquery - 使用 jQuery 在网页中模仿 CLI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3654315/

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