你好StackOverflow的人,我在嘗試這個問題之前確保通過網站徹底準備好,也鼓起足夠的勇氣尋求幫助(我喜歡解決我自己的問題,但在這種情況下,我來到了一個難關)。jQuery加載(),加載不正確。我怎樣才能解決這個問題?
所以我需要的「幫助」是批評和指導,我不認爲我有足夠的知識來找到問題。到目前爲止,我正在嘗試一個從用戶接收數據並輸出結果的網站,我完成了這個工作,但是當我按下提交時出現問題,它刷新了我不想要的頁面。我得出了使用按鈕並鍵入'按鈕'以防止它令人耳目一新的結論,太棒了!現在用戶可以輸入他/她的信息,它會完美地吐出結果。現在,這是問題出現的地方(儘管結果是正確的),但我希望我的網站允許用戶選擇不同的選項(如果他/她決定的話),所以當我從下拉菜單中選擇另一個選項時按下並按下提交,它什麼都不做。
這導致我研究這個問題,它導致我有幾種可能性,jQuery .load()不會因爲本地和web服務器問題而加載?我想把它放在那裏,我不使用數據庫,這只是一個簡單的網站,根據用戶的信息吐出結果(我會提供底部的代碼片段)。此外,在控制檯中我得到這個消息:
BMI_ws.html?age=&weight=&height=:44 Uncaught TypeError: BMR is not a function
at display_gains (bmi_backend.js:27)
at HTMLButtonElement.onclick (BMI_ws.html?age=&weight=&height=:44)
,我發現研發的另一件事是XML的提的是,和AJAX,可以幫助我解決這個問題,但我只是想這成爲一個沒有數據庫的簡單網站,如果我可以使用jQuery來正確操縱它,我會很高興。
HTML代碼:
<!DOCTYPE HTML>
<html>
<head>
<title> Simon's BMi Tool</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src = 'jQuery_methods.js'></script>
<link rel = 'stylesheet' href = 'style.css'/>
<script src = 'bmi_backend.js'></script>
</head>
<body>
<div id = 'container'>
<div id = 'header'>
<h1>Simon's BMi Tool</h1>
</div>
<div id = 'introduction' >
<div id='context'>
<h2> Instructions: <h2>
<p> To properly use this to its best potential, you should accurately put in your information so that it can provide you an accurate daily caloric intake. </p>
</div>
</div>
<div id = 'content'>
<div id = 'input'>
<h2> Caloric Calculator <h2>
<form name = 'Values' >
<input id = 'm' type ='radio' name = 'gender' value = 'Male'> Male <br>
<input id = 'f' type ='radio' name = 'gender' value = 'Female'> Female <br>
<br> Age: <br> <input id = 'a' type ='number' name = 'age' min = '10' max = '90' placeholder ='Age #'> <br>
<br> Weight(lbs) <br> <input id = 'w' type ='number' name ='weight' min = '70' max = '250' placeholder ='Weight #'> <br>
<br> Height(inches) <br> <input id = 'h' type ='number' name ='height' min ='36' max = '84' placeholder ='Height #'> <br>
<br> Activity Level: <br> <select id = 'activity'>
<option value ='0'>Basic Metabolic Rate</option>
<option value ='1'>Desk Job</option>
<option value ='2'>Lightly Active</option>
<option value ='3'>Moderately Active</option>
<option value ='4'>Active</option>
<option value ='5'>Extremely Active</option>
</select>
<button type = 'button' onclick ="display_gains()"> submit </button>
</form>
</div>
<div id= 'placeholder'>
<p>Maintenence: </p><p id = 'maint_daily'>0</p>
<p>Lose .5 per week: </p> <p id = 'lose_half'>0</p>
<p>Lose 1 per week: </p> <p id = 'lose_one'>0</p>
<p>Gain .5 per week: </p> <p id = 'gain_half'>0</p>
<p>Gain 1 per week: </p><p id ='gain_one'>0</p>
</div>
</div>
<div id = 'footer'>
Copyright © 2017 Simon Liu
</div>
</div>
</body>
</html>
的JavaScript:
var age;
var height;
var weight;
var activity;
var BMR;
var selection;
function BMR()
{
if(document.getElementById('m').checked)
{
age = document.getElementById('a').value;
height = document.getElementById('h').value;
weight = document.getElementById('w').value;
BMR = male_bmr(age, height, weight);
} else
{
age = document.getElementById('a').value;
height = document.getElementById('h').value;
weight = document.getElementById('w').value;
BMR = female_bmr(age, height, weight);
}
return BMR;
}
function display_gains(){
var value = BMR();
var option = document.getElementById('activity');
var text = option.options[option.selectedIndex].text;
if (text === 'Basic Metabolic Rate')
{
return document.getElementById('maint_daily').innerHTML = value.toFixed(0);
}
else if (text === 'Desk Job')
{
document.getElementById('maint_daily').innerHTML = (value*1.2).toFixed(0);
document.getElementById('lose_half').innerHTML = (one(value) - 250).toFixed(0);
document.getElementById('lose_one').innerHTML = (one(value) - 500).toFixed(0);
document.getElementById('gain_half').innerHTML = (one(value) + 250).toFixed(0);
document.getElementById('gain_one').innerHTML = (one(value) + 500).toFixed(0);
}
}
function male_bmr(a, h, w){
return(66.47 + (13.75 * (kg(w))) + (5 * (cm(h))) - (6.75 * a));
}
function female_bmr(a, h, w){
var fbmr = 665.09 + (9.56 * (kg(w))) + (1.84 * (cm(h))) - (4.67 * a);
return fbmr;
}
function kg(lbs){
var ans = (lbs * .453592);
return ans;
}
function cm(inches)
{
var ans = (inches) * (2.54);
return ans;
}
function one(val)
{
var sample = val;
return sample*1.2;
}
function two(val)
{
return val*1.375;
}
function three(val)
{
return val*1.55;
}
function four(val)
{
return val*1.725;
}
function five(val)
{
return val*1.9;
}
最後是的jquery.js:
$(document).ready(function(){
$("button").click(function(){
$("#placeholder").load("bmi_backend.js");
});
});
這些是我的網站工作的代碼,我確實有CSS也是如此,但我認爲問題不在於此。如果有人能指導我解決這個問題,或者指導我正確的道路,那麼我可以研究它並自己解決它,我將不勝感激!謝謝!
P.S對不起,這是一個可怕的帖子,這是我第一次發佈。
第一步通常是查閱文檔。你有沒有回顧[jQuery.load()](http://api.jquery.com/load/)? –