gpt4 book ai didi

javascript - 验证不起作用,现在我的网站也坏了

转载 作者:行者123 更新时间:2023-12-03 11:37:08 25 4
gpt4 key购买 nike

我的基本知识“插入数字,输出答案”方面工作正常。但我需要在网站中进行验证,以便只有某些功能有效。我的网站在进行验证后完全停止工作,我不完全明白为什么或发生了什么。

JS fiddle :http://jsfiddle.net/ufs869wu/

HTML:

 <form id="form1" name="form1" method="post" action="">
<label for="txtAge">Age:</label>
<input type="text" class="txtInput" id="txtAge" value="0"/><p id="ageRes"></p>
<br/>
<label for="txtMass">Mass in Lbs:</label>
<input type="text" class="txtInput" id="txtMass" value="0"/>
<br/>
<label for="txtHinch">Height in Inches:</label>
<input type="text" class="txtInput" id="txtHinch" value="0"/>
<br/>
<input type="button" id="btnCalc" value="Calculate"/>
<p id="result2">Result</p>
</form>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="BMI.js"></script>
</body>

和JS

// JavaScript Document

$(function () {
//Identify Variables
var txtMass, txtHinch, result;
var isValid = $('#form1').validate().form();
// attach event listener to the toggle button's click event



$('#btnCalc').click(function () {

//Set validator
$.validator.setDefaults({
errorElement: "span",
errorClass: "form_error",
errorPlacement: function(error,element){
error.insertAfter(element)
}
});
$.extend($.validator.messages,{
required: "* Required field"
});
//Set Validation perameters
$("#form1").validate({
rules: {
txtAge: {
required: true,
range: [1, 120],
digits: true
},
txtMass: {
require: true,
digits: true
},
txtHinch: {
requre: true,
digits: true
}
}
});

if (isValid) {

//Set Age range for form accuracy
if (txtAge < 16 || txtAage > 80){
//Output
$('#ageRes').html('Results may not be accurate at your age')
} else { (txtAge >= 16 || txtAge <= 80)
$('#ageRes').html('Results should be accurate considering your age')




//Equation for BMI
result = ($('#txtMass').val() / ($('#txtHinch').val() * $('#txtHinch').val())) * 703;}

//If - Else statement from output of BMI equation

if (result < 16){
$('#result2').html('Result: '+result.toFixed(1) + ' you are Severely underweight')
} else if (result <=18 ){
$('#result2').html('Result: '+result.toFixed(1) + ' you are underweight')
} else if (result <=24){
$('#result2').html('Result: '+result.toFixed(1) + ' you are healthy')
} else if (result <= 30 ){
$('#result2').html('Result: '+result.toFixed(1) + ' you are seriously overweight')
} else if (result <=35 ){
$('#result2').html('Result: '+result.toFixed(1) + ' you are obese')
} else if (result <=40 ){
$('#result2').html('Result: '+result.toFixed(1) + ' you are seriously obese')
}

}


});


});

感谢您的帮助!

最佳答案

您在加载 jquery 之前调用“$”,并且收到“$”未定义错误。尝试将此行移动到 html 的 head 部分。

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

此外,您是否在某处包含了 jquery 验证插件?我没有看到它被包含在任何地方。

关于javascript - 验证不起作用,现在我的网站也坏了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26444701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com