2015-10-06 88 views
1

我在寫一個調用JQuery腳本內的PHP函數的程序。 代碼第一次工作,但是當我嘗試再次使用相同的PHP它不起作用。重複PHP和JQuery函數調用

下面是代碼:

$("#left").click(function() { 
    $("#Left").text("You chose to go left"); 
    $("#Right").remove("#Right"); 
    $("#result").html("<?php left(); ?>"); 
}); 

$("#right").click(function() { 
    $("#Left").remove("#Left"); 
    $("#Right").text("You chose to go Right"); 
    $("#result").html("<?php right(); ?>"); 
}); 

$("#fight").click(function() { 
    $("#Fight").text("You chose to fight"); 
    $("#Run").remove("#Run"); 
}); 

$("#run").click(function() { 
    $("#Fight").remove("#Fight"); 
    $("#Run").text("You chose to run"); 
}); 

左和右的工作就好了,但是打,跑不爲別人工作的原因所在。

+1

我投票作爲題外話,因爲它是http://stackoverflow.com/questions的可能重複關閉這個問題/ 203198/event-binding-on-dynamic-created-elements –

+0

原因是你正在處理動態元素 –

+0

'$(「#Right」)。remove(「#Right」);'和$( 「#Right」)。remove();' –

回答

1

問題與事件委託改變你這樣的代碼,

$(document).on("click","#left",function() { 
    $("#Left").text("You chose to go left"); 
    $("#Right").remove("#Right"); 
    $("#result").html("<?php left(); ?>"); 
}); 

$(document).on("click","#right",function() { 
    $("#Left").remove("#Left"); 
    $("#Right").text("You chose to go Right"); 
    $("#result").html("<?php right(); ?>"); 
}); 

$(document).on("click","#fight",function() { 
    $("#Fight").text("You chose to fight"); 
    $("#Run").remove("#Run"); 
}); 

$(document).on("click","#run",function() { 
    $("#Fight").remove("#Fight"); 
    $("#Run").text("You chose to run"); 
}); 
+0

工作得很好。謝謝 – Dexstrum

+0

我們只是改變了點擊功能的代表團..它打了工作 –

+0

很高興它的工作:) –