2015-07-20 26 views
-1

我有一個頁面searchKB.jsp,它有一些像這樣的代碼:無法應用的jQuery

$.ajax({ 
    type: "GET", 
    data: {app:app,env:env,ptitle:ptitle,kbaseId:kbaseId}, 
    url : 'jsp/knowledgeBase/kbResults.jsp', 
    success: function(res){ 
     getResponse(res); 
    }, 
    error: function(){ 
     document.getElementById("message").style.display=""; 
     $('#message').html("<b><font color=red face=Arial size=2>An Error encountered while processing your Request.Please try again after sometime.</font></b>"); 
    } 
}); 
function getResponse(response){ 
    document.getElementById("message").style.display="none"; 
    document.getElementById("KBInfo").innerHTML = response; 
} 

searchKB.jsp呼籲kbResults.jsp通過上面的代碼,現在我要上的內容適用的jQuerykbResults.jsp ..我會怎麼做?

我什麼都試過,但未能

<input type="button" id="expand3" value="Hide"/> <div id="result3">hide this</div>

和相應的jQuery代碼

$("#expand3").click(function(){ $("#result3").hide(); });

+0

你是什麼意思「應用jquery」? – karthikr

+0

您應該首先在dom中的某處添加kbResults.jsp。 – nikhil

+0

我的意思是我想使用'hide()'和其他函數 –

回答

2

假設使用標識expand3result3元件由AJAX調用來了,你可以做這樣的事情:

$.ajax({ 
    type: "GET", 
    data: {app:app,env:env,ptitle:ptitle,kbaseId:kbaseId}, 
    url : 'jsp/knowledgeBase/kbResults.jsp', 
    success: function(res){ 
     getResponse(res); 
    } // removed the error callback for clarity 
}); 
function getResponse(response){ 
    $("#message").hide(); 
    $("#KBInfo").html(response); 

    // only then attach the listener 
    // because #expand3 needs to be in the DOM 
    $("#expand3").click(function(){ 
     $("#result3").hide(); 
    }); 
} 

或者,你可以使用@ ashokd與委託監聽的解決方案。

+1

只有你的解決方案工作的人 –

+0

我很高興我可以幫助......雖然我非常確定@ ashold的解決方案應該也可以。 – Shomz

4

嘗試使用這樣的事情。

$(document.body).on("click", "#expand3", function(){ 
    $("#result3").hide(); 
}); 
+0

不工作,已經嘗試了它 –

+0

@amolsingh是否點擊事件被解僱? – ashokd

+0

@amolsingh更新答案,看看這是否有效。 https://jsfiddle.net/vwmds5f8/ – ashokd