您需要使用AJAX
:
<script type="text/javascript">
if (confirm('Are you sure?')) {
var request = window.XMLHttpRequest() ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
request.open("POST", "phpfile.php", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () {
if (request.readyState == 4 && (request.status == 200 || request.status == 0 /*Fixes a FF bug*/)) {
alert(request.responseText); // should return "example1-example2"
}
}
request.send("data1=example1&data2=example2"); // you can send data here
}
</script>
phpfile.php:
<?php
return $_POST['data1'] . "-" . $_POST['data2'];
?>
我真的不喜欢传统 AJAX 与使用库相比有多么复杂,但希望这能给您带来想法。