【发布时间】:2017-03-27 20:32:12
【问题描述】:
我希望它只“重新加载”到#searchuserc。查了很多资料,还是没能解决问题。
$(document).ready(function() {
$("#onlineusers").load("online.php");
$("#searchuserc").show();
setInterval(function() {
$("#onlineusers").load("online.php");
}, 5000);
$(".search").click(function() {
$("#onlineusers").hide();
e.preventDefault(); // Prevent Default Submission
$.ajax({
url: 'searchuser.php',
type: 'POST',
data: $(this).serialize(), // it will serialize the form data
dataType: 'html'
})
.done(function(data) {
$('#searchuserc').fadeOut('slow', function() {
$('#searchuserc').fadeIn('slow').html(data);
});
})
.fail(function() {
alert('Ajax Submit Failed ...');
});
});
});
这是我的 HTML 文件
<form method="POST">
<input type="text" id="searchuser" name="searchuser" placeholder="Sök efter en användare..." />
<input type="submit" class="search" name="searchbtn" value="Sök" />
</form>
<div id="searchuserc">
</div>
【问题讨论】:
-
你调用了e.preventDefault()但没有定义e,检查你的js控制台是否有错误,我认为你需要$(".search").click(function(e){跨度>
-
$(this).serialize()- 点击的按钮? -
也许把
$('#searchuserc').fadeOut('slow', function() { $('#searchuserc').fadeIn('slow').html(data); });改成$('#searchuserc').fadeOut('slow', function() { $(this).html(data); }).fadeIn('slow');
标签: javascript jquery ajax