5 回答

TA貢獻1772條經驗 獲得超6個贊
您可以嘗試隱式調用該函數
<html>
<meta charset="UTF-8">
<body>
<button id="testbutton" type="button">
Click Me
</button>
<p id="p"></p>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$('body').on('click', '#testbutton', function(){
$.ajax({
type : 'POST',
url : 'testing.php',
success : function(data) {
alert(data);
}
});
});
</script>
</body>

TA貢獻1893條經驗 獲得超10個贊
在我看來,你有3件事需要解決:
您缺少函數的開始標記,因為您目前擁有的開始腳本標記是針對您正在引用的 jquery 庫的。
<script>
此外,不要使用保留字“click”作為函數名稱。我已將其更改為“我的功能”
將函數定義移動到頁面中的適當位置。
如果您嘗試下面的代碼,它應該可以正常工作。我希望這有幫助。
<html>
<meta charset="UTF-8">
<body>
<script>
function myclick(){
alert('posting!');
$.ajax({
type: 'POST',
url: 'testing.php',
success: function(data) {
alert(data);
}
});
}
</script>
<button type="button" onclick="myclick()">Click Me</button>
<p id="p"></p>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"/>
</body>
</html>

TA貢獻1828條經驗 獲得超4個贊
我建議這樣:(將它而不是你的代碼替換成Body標簽。
<button type="button" id="ajaxBtn">Click Me</button>
<p id="p"></p>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
const btn=document.getElementById('ajaxBtn');
btn.addEventListener('click',click);
function click(){
$.ajax({
type: 'POST',
url: 'testing.php',
success: function(data) {
alert(data);
}
});
}
</script>

TA貢獻1797條經驗 獲得超6個贊
您的代碼有一些問題:首先,它不是一個正確的HTML文件。每個 HTML 文件都應該有一個 標記,并且 標記中應包含 標記。<head></head><body></body><html></html>
其次,您希望在 部分中加載腳本。您還可以在其中定義標題,元標記,樣式表等。<head>
第三,你的標簽是錯誤的。加載腳本,同時定義函數。這應該是兩個操作。<script>
我認為你的腳本會看起來像這樣:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</head>
<body>
<button type="button" onclick="click()">Click Me</button>
<p id="p"></p>
</body>
<script>
function click(){
$.ajax({
type: 'POST',
url: 'testing.php',
success: function(data) {
alert(data);
}
});
}
</script>
</html>
有關 HTML 的信息,請參閱 W3 學校
- 5 回答
- 0 關注
- 201 瀏覽
添加回答
舉報