2 回答
TA贡献1828条经验 获得超6个赞
如上所述,发出 AJAX 请求的最简单方法可能是尝试 jQuery:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- Add jQuery on your HTML page -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<!-- Add some custom JavaScript file -->
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form id="warmupForm" class="form">
<label for="userWorkLoad">Work load (in kgs)</label><br>
<input type="text" name="userWorkLoad" id="userWorkLoad">
<button id="btn" type="submit">Calculate</button>
</form>
<div id="output">This is where I want the result to be shown with AJAX</div>
</body>
</html>
内容script.js:
$(function() {
// Process a button click
$("#btn").click(function() {
event.preventDefault();
// Get input field
var userWorkLoadInput = $("#userWorkLoad");
// Build some request parameters
var params = {
userWorkLoad: userWorkLoadInput.val()
};
// Let's name your PHP script file as "server.php"
// And send POST request with those parameters
$.post("server.php", params, function(response) {
// Response text we're going to put into the `output`
$("#output").html(response);
});
});
});
TA贡献1891条经验 获得超3个赞
您可以简单地使用 Jquery 而不是使用 Ajax 来完成此操作(使用 PHP,您应该将 method="POST" 添加到表单中)。这是一个例子:
$(document).ready(function(){ $("#send").click(function(){// your calculates$("#output").html(...); }); }); ... <button type="submit" id="send">Calculate</button>
- 2 回答
- 0 关注
- 93 浏览
添加回答
举报