jQuery没有在转发的页面上工作

时间:2015-07-17 05:41:19

标签: javascript jquery jsp

我正在测试一些jQuery。我写了一个简单的jsp页面,在提交时使用jQuery并返回结果

代码如下:

<head>
<script type="text/javascript">
 $(function(){
       $("#hide2").click(function()
       {
           $("#result2").hide();    
       });   
     ///////////////////////        
     //some jquery code (running fine)
      /////////////////////
        });     // enable ..show ..hide 

       $(document).ready(function() 
        {
        $('#form').submit(function() {

    var number = $('#number').val();

    $.ajax({
        type:       "post",
        url:        "test5.jsp",
        data:       "number=" + number,
        success:    
                   function(msg) 
                    {
                          $("#result2").html("<h3>" + msg + "</h3>").fadeIn("slow");
        }
    });
return false;
});
});
    </script>
</head>
<body>
<form id="form" action="" method="post">
Enter number:
    <input id="number" type="text" name="numb" disabled/>
    <input id="enable" type="button" value="Enable">
    <input id="show" type="button" value="show">
     <input id="hide" type="button" value="hide">
<input id="submit" type="submit" value="Calculate Square Root"    name="submit"/>
</form>
<p id="result"></p>
<p id="result2"></p>
</body>

输入值并提交请求后,请转到test5.jsp

<%
 //java code (running fine)  
%>
<html>
<head>
   <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//malsup.github.com/jquery.form.js"></script>
<script>

</script> 
</head>
<body>
   <br><br> 
    <button id="hide2" value="Hide">Hide</button> --> <span id="result2">hide this</span>
</body> 
</html>

现在我想在隐藏按钮上应用jQuery,但我无法这样做。简而言之,我想将jQuery应用于test5.jsp

的元素

before

after

1 个答案:

答案 0 :(得分:0)

好的,所以要在成功回调中从ajax更改html(响应),你需要做类似下面的事情,

在你成功的回调中, 成功:

 function(msg) 
 {
   //...... rest of your code
   $(msg).find("#hide2").hide();  //Hides #hide2 button from msg response

 }