javascript - 从作为ajax响应加载到div中的页面中删除html元素

标签 javascript jquery html ajax jsp

一旦我的 index.jsp 页面作为单击 regdata.jsp 页面上的登录按钮的响应加载,我就尝试删除带有 id=regbutton 的链接(我想将其设为按钮,也请帮忙)

regdata.jsp

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#mainDiv").load("index.jsp");
        $('#regButton').remove();
    });
});
</script>
</head>
<body>
<div id="mainDiv" class="units-row">
<h2>Thanks For registering!</h2>
<button id="regLogin">Login Now</button>
</div>

index.jsp

<body>
       <center>
         <s:actionerror />
         <h1 id="login_title"></h1>
         <form action="checkLogin.action" method="post">
            <p>
               <input type="text" id="username" name="username" 
                  placeholder="" >
            </p>
            <p>
               <input type="password" name="password" 
                  placeholder="" id="password">
            </p>
            <label> <input type="checkbox" name="remember_me" id="remember"><label for="remember" id="rememberlbl"></label></label>
            </p>
            <input type="submit" class="btn-log" method="execute"
               key="label_login"> &nbsp;&nbsp;
         </form>
         <a href="registerLogin.action" id="regButton">Register</a>         
      </center>
   </body>

最佳答案

jQuery 的 load函数采用第二个或第三个参数 - 加载完成时执行的回调函数。因此,如果您希望按钮仅在加载完成时消失,您应该将其代码移至该处理程序中:

$(document).ready(function(){
    $("button").click(function(){
        $("#mainDiv").load("index.jsp", function() {
          $('#regButton').remove();
        });
    });
});

关于javascript - 从作为ajax响应加载到div中的页面中删除html元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31177027/

相关文章:

javascript - 如何保存 html 表单输入数据?

javascript - POST 数据未使用 XMLHttpRequest 发送

javascript - 当某个输入为空时创建另一个按钮

javascript - 比较 URL 和 JavaScript

javascript - JS 搞乱了幻灯片的定位

javascript - 获取图标的父 div ID onclick

html - 我不需要对旧版浏览器的支持,那么 IE11 是否需要 modernizr?

php - JSON 结果用于选择选项

javascript - Cookie 不会在 POST 请求时随 JavaScript 一起发送

javascript - JS/Jquery 替换内部文本而不破坏事件