使用jQuery刷新div
问题描述:
是否可以使用jQuery刷新单个div?我有一个注销用户的按钮。单击按钮后,我想显示登录表单,所以我需要重新解析这个div。
Is it possible to refresh a single div with jQuery? I have a button that does logout a user. After the button is clicked, I want to show the login form, so I need to re-parse this div.
我的div的内容是:
<div id="signup" class="register">
<?php
$email = $_COOKIE['email'];
$login = $_COOKIE['login'];
if($email != null && $email != "" && $login != null && $login != "") {
echo "<h3>you are logged in as <b>$email</b></h3><br><br><button id='logoutbtn' class='submitButton'>Logout</button>";
} else {
// show login form
}
?>
</div>
这是我执行注销的JavaScript部分:
This is my JavaScript part where I perform the logout:
$('#logoutbtn').click(function () {
$.ajax({
type: "POST",
url: "php/logout.php",
async: false,
success: function(data){
// reload signup div
}
});
});
编辑:我的logout.php脚本删除了cookie,这就是我需要重新解析注册的原因-div。
My logout.php script removes the cookies, that's why I need to re-parse the signup-div.
答
您可以使用jquery动态更改注册div的内容,如下所示:
You can dynamically change the contents of the signup div with jquery as follows:
$("#signup").html("My New Login Section");
或
var $loginDiv = $(document.createElement("div")).html("<label>Username</label><input type='text' id='username'/>");
$("#signup").html($loginDiv);