2015-12-15 60 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>