【发布时间】:2020-04-23 12:31:28
【问题描述】:
以 JSON 格式返回数组的 Ajax 请求。遍历返回的数组以从其中一列中吐出条目并使用 html div 显示。
问题是结果只在网页上显示一纳秒然后消失。
我需要在那里防止默认设置吗?大括号等是否在正确的位置?
JQuery sn-p:
$(document).ready(function(){
$(":submit").click(function(e){
var msg = $("#search").val();
$.ajax({
url:'search.php',
method:'POST',
data:{
msg:msg
},
dataType: 'json',
success: function(response) {
for( var key of Object.keys( response ) ) {
$( '.content' ).append( `<div class="post"><div class="post-text">${response[key].MessageText}</div></div>` );
}
}
});
});
});
HTML:
<form action="index.php" method="post" autocomplete="on"><pre>
<input name="msg" id="search" type="text" autofocus value= "<?php if(isset($_POST['msg'])) {
echo htmlentities ($_POST['msg']); }?>"></input> <span id="error"></span>
<input type="submit" id="submit" style="border:0; padding:0; font-size:0">
</pre></form>
<div class="content"></div>
【问题讨论】:
-
我猜你会在短时间内看到那里的值,因为一旦你提交它就会同时刷新浏览器中的页面。
-
@norbitrial 这很有意义。 :)
-
如果您仍然需要帮助,那么如果您发布 jQuery 为
:submit找到的 HTML 元素,很乐意提供帮助。 -
谢谢@norbitrial 非常感谢。已将 html 代码添加到原始问题中。
标签: jquery html arrays json ajax