【发布时间】:2020-05-03 18:53:31
【问题描述】:
当我有很多按钮 1 页面时,如何识别发出不同 AJAX 请求的不同按钮?请参见下面的代码/脚本(button1 到 switch_on.php,button2 到 switch_off.php)。如何将特定按钮链接到 1 页内的特定功能?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Update MySQL</h2></div>
<button switch-ONid=“1” class="loadButton">Switch ON</button>
<button switch-OFFid=“0” class="loadButton">Switch OFF</button>
</body>
</html>
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "switch_on.php",
success: function(result){
$("#div1").html(result);
}
});
});
});
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "switch_off.php",
success: function(result){
$("#div1").html(result);
}
});
});
});
</script>
【问题讨论】: