gpt4 book ai didi

javascript - $ ("#input").val() 在使用 $.post() 时返回未定义

转载 作者:行者123 更新时间:2023-11-27 22:57:47 24 4
gpt4 key购买 nike

我觉得无法解决这个问题很愚蠢,但我的发帖请求一直在 $('#input').val() 行中显示为未定义。有人可以帮我解决这个问题吗?

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
let sr = $("#input").val();
$(document).ready(function(){
$("button").click(function(){
$.post("http://localhost:8080/", {subreddit: sr}, function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<div id='content' style="height:100%, width:100%">
<input id='input' type="text" placeholder='enter subreddit'/>
<button>Search</button>
</form>
</div>
</body>
</html>

最佳答案

这是因为您的脚本部分在加载 DOM 之前正在运行。将它移到你的 $(document).ready() 中:

$(document).ready(function() {
let sr = $("#input").val();
//Rest of your code
});

关于javascript - $ ("#input").val() 在使用 $.post() 时返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54489683/

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