2015-12-15 61 views
0

我正試圖編寫單擊按鈕時寫入文件的代碼。我開始讓JavaScript的(和jQuery)調用PHP文件實驗:Javascript調用php?

<!DOCTYPE html> 
<html> 
<title> 
Test Page 
</title> 
<head> 


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 

<button id="ST1U" onclick="feedback(this)">I understand</button> 
<button id="ST1N" onclick="feedback(this)">I don't understand </button> 
<script type="text/javascript"> 
function feedback(buttonElement){ 
var bcid= buttonElement.id; 
if(bcid==='ST1U'){ 

    $('#output').load('form.php'); 
} 
else if(bcid==='ST1N'){ 
    alert("We are sorry you didn't understand") 
}} 
</script> 

,並在文件 'form.php的':

<script type-'text/javascript'> 
alert('thank you for your responds') 
</script> 

然而,當我點擊第一個按鈕,php腳本似乎沒有運行,因爲我沒有看到警報消息。任何想法,爲什麼它不工作?

+3

我在代碼中看不到「id =」輸出。 – Sean

+0

您的代碼有效,但您需要。 –

回答

3

你已經要求jquery加載輸出id,元素在哪裏?

地址:

<div id="output"></div> 

所以,你的代碼:

<!DOCTYPE html> 
<html> 
<title> 
Test Page 
</title> 
<head> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 

<button id="ST1U" onclick="feedback(this)">I understand</button> 
<button id="ST1N" onclick="feedback(this)">I don't understand </button> 
<div id="output"></div> 
<script type="text/javascript"> 
function feedback(buttonElement){ 
var bcid= buttonElement.id; 
if(bcid==='ST1U'){ 
    $('#output').load('123.php'); 
} 
else if(bcid==='ST1N'){ 
    alert("We are sorry you didn't understand"); 
}} 
</script> 
-2

,而不是反饋(本),你可以使用反饋( 'ST1U')通過元素的ID。糾正你的腳本標籤

+4

使用'this'確實有什麼問題?他們通過'ele.id'從傳入的元素中獲取'id'。 –

-1

您可以使用jQuery的,而不是JavaScript的....

<!DOCTYPE html> 
<html> 
<title> 
Test Page 
</title> 
<head> 


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 

<button id="ST1U" class="button">I understand</button> 
<button id="ST1N" class="button">I don't understand </button> 
<script type="text/javascript"> 
$(document).ready(function(){ 

$(".button").click(function(){ 

var bcid= $(this).attr("id"); 
if(bcid==='ST1U'){ 

    $('#output').load('form.php'); 
} 
else if(bcid==='ST1N'){ 
    alert("We are sorry you didn't understand") 
} 
}); 

}); 

+0

仍然沒有'#輸出'元素。 – Biffen

0

試試你的代碼中加入輸出ID,我在我的本地代碼工作正常進行了測試,可能你忘了放置輸出ID ..

<!DOCTYPE html> 
<html> 
<title> 
Test Page 
</title> 
<head> 


    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> 

    <button id="ST1U" onclick="feedback(this)">I understand</button> 
    <button id="ST1N" onclick="feedback(this)">I don't understand </button> 
    <div id="output"></div> 
    <script type="text/javascript"> 
    function feedback(buttonElement){ 
    var bcid= buttonElement.id; 
    if(bcid==='ST1U'){ 

     $('#output').load('form.php'); 
    } 
    else if(bcid==='ST1N'){ 
     alert("We are sorry you didn't understand") 
    }} 
    </script>