【发布时间】:2017-01-11 21:42:21
【问题描述】:
更新:我加了“#”后还是不行。
我是 ajax 新手。我正在练习将值发送到 php 脚本,并返回结果。
现在,我遇到了一个问题,我无法在我的 html 页面中显示我的结果。 我尝试在线提供答案,但我仍然无法解决此问题。
我的 index.html 从表单中获取值并将表单信息发送到 getResult.php。
我的 getResult.php 将进行计算和回显结果。 如何在 index.html 中显示结果?
她的是html代码
index.html
<html>
<body>
<form name="simIntCal" id="simIntCal" method="post"
>
<p id="Amount" >Amount(USD)</p>
<input id="amount_value" type="text" name="amount_value">
<p id="annual_rate" >Annual Rate of Interest
(%)</p>
<input id="rate_value" type="text" name="rate_value">
<p id="time_years" >Time (years)</p>
<input id="time_value" type="text" name="time">
<input id="calculate" type="submit" value="Calculate">
</form>
<p id="amount_inteCal" >The Amount (Acount
+ Interest) is</p>
<input id="result" type="text">
</body>
</html>
ajax 脚本:
<script>
$('#simIntCal').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'getResult.php',
data: $('#simIntCal').serialize(),
success: function (result) {
$("#result").text(result);// display result from getResult.php
alert('success');
}
});
});
</script>
getResult.php
<?php
if ($_SERVER ["REQUEST_METHOD"] == "POST") {
//do some calculation
$result=10;//set result to 10 for testing
echo $result;
}
?>
【问题讨论】:
-
在你的成功回调中应该是
$("#result")。 -
$("#result").val(result);
-
@ccKep@Ivan Barayev,我加#后还是不行。
-
浏览器调试工具 (F12) > 网络选项卡显示为请求/响应是什么?
-
@ccKep 它没有显示任何东西,但它给了我 Uncaught ReferenceError: $ is not defined in console.