我正在开展一个学校项目,并尝试创建一个计算器,可以在以下链接找到:
http://jsfiddle.net/ae97vgxz/2/
我的JS是:
$(document).ready(function(){
// Setup variable as empty
var method = "";
// Detect when initial radio button is clicked
$("input[type=radio]").click(function() {
// Get the weight from the input box
var weight = $("#meatWeight").val();
// If the water method was clicked
if ($(this).hasClass("water")) {
var method = weight * 60;
// Show me what the value is (can be removed)
alert(method);
// If the fridge method was clicked
} else if ($(this).hasClass("fridge")) {
var method = weight * 793;
// Show me what the value is (can be removed)
alert(method);
}
});
当您使用它时,如果您先输入一个重量,然后选择一个解冻方法,您将在警告窗口中获得正确的答案。但是,如果您按下“计算”'按钮,您会收到以下消息 -
{"error": "Please use POST request"}
从我自己的一些研究中,我相信这是因为我试图提交一个表格,而JSFiddle并没有让你这样做。如果我在Chrome中尝试使用本地环境,则无法再输出。
我的JS知识非常有限(因为我确定你可以看到)所以我无法理解解决方案。任何人都可以建议我做错了什么以及解决方案可能是什么?
谢谢!
答案 0 :(得分:1)
你在这里犯了错误:
function defrost(weight) {
return (makeTime(method));
}
应该是:
function defrost(weight) {
return (makeTime(weight));
}
此外,您应该更改makeTime函数,否则它将无法正常工作。 parseInt子句应该是这样的:
parseInt(time / 60);
答案 1 :(得分:0)
您目前提交表单的方法是GET
<form id="defrostCalculator" name="defrostCalculator" onsubmit="callbackDefrost(this.elements.meatWeight.value); return false;" method="GET">
如果目的地需要POST,请使用POST。
<form ... method="POST">
答案 2 :(得分:0)
您的代码有一些小问题。但是,您遇到的主要问题是,如果您想在jsfiddle上使用非AJAX表单,则必须更改所有按钮以具有该属性:
type="button"
而不是您目前所拥有的:
type="submit"
当您执行<input type="submit"
时,jsfiddle.net会因为您无法向其服务器提交表单信息而发出警告并失败。幸运的是,您不需要任何AJAX或服务器交互。所以你的简单计算器只需使用简单的按钮。