我在拍摄这段时间时遇到了麻烦,我对这个程序无法正常工作的原因感到困惑。我正在学习一门入门的JavaScript课程,所以我确信这可能是我错过的一些显而易见的事情,但我们将非常感谢您的帮助。
这是javascript / jquery
$(document).ready(function(){
$("#form1").validate({
rules: {
salary: "required",
creditScore: "required",
months: "required"
},
messages: {
salary:"salary is required",
creditScore:"credit score is required",
months: " months is required"
}
});
$("#form1").submit(function(){
});
$('#verify').click(function() {
'use strict'
var form = $("#form1");
form.validate();
if (form.valid()) {
var salary = $("#salary").val();
var creditScore = $("#creditScore").val();
var months = $("#months").val();
var result;
console.log (salary);
console.log (creditScore);
console.log (months);
if ( salary >= 40000 )
{
if(creditScore >= 600 || months > 12)
{
result = "Loan approved";
}
}
else if (salary < 40000 )
{
if (creditScore >= 600 && months > 12)
{
result = "Loan approved.";
}
}
else
{
result = "Loan denied.";
}
console.log(result);
$('#output').val(result);
return false;
}
});
});
这是html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="jquery.validate.js"></script>
<script src="LoanProcess.js"></script>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="salary">salary: </label>
<input type="text" name="salary" id="salary">
</p>
<p>
<label for="creditScore">creditScore: </label>
<input type="text" name="creditScore" id="creditScore">
</p>
<p>
<label for="months">months: </label>
<input type="text" name="months" id="months">
</p>
<p>
<input type="submit" name="verify" id="verify" value="Verify">
</p>
<p>
<input type="text" name="output" id="output" value="">
</p>
</form>
<div id="result">
</div>
</body>
</html>
</body>
</html>
答案 0 :(得分:1)
几个笔记:
您的脚本标记不在标记之前且在正文之前 - 它们必须在一个或另一个内部
body和html标签都关闭了两次
如果您想使用'use strict'
,通常将其作为模块的第一行,即使将其置于函数内是完全合法的,在您的情况下也没有多大意义
如果您不需要提交表单的处理程序,请将其删除
使用jquery的良好做法是缓存您将多次使用的选择器:
$()。val()返回字符串,因此您必须先将其转换为数字,然后再与数字进行比较,使用parseInt
,parseFloat
,+x
,x | 0
或其他适合您案例的方法
如果外部条件为真但内部不是
load denied
。
考虑例子:
$(function() {
var form = $('#form')
, $salary = $("#salary")
, $creditScore = $("#creditScore")
, $months = $("#months")
, verify = $('#verify')
form.validate(...)
verify.on('click', function(e) {
form.validate()
if (form.valid()) {
// I belive you are waiting for integers more than a zero
// so using `x | 0` you are getting 0 or integer
var salary = $salary.val() | 0
, creditScore = $creditScore.val() | 0
, months = $months.val() | 0
// now you have numbers so your comparison would number with number not string with string
...
}
})
})
还要感谢@Tieson T有一个小提琴,似乎正在做你想做的事http://jsfiddle.net/A4Rg3/2/
答案 1 :(得分:1)
验证您的条件,对于其中一些您没有指定要设置结果的其他声明。
EG。
if ( salary >= 40000 )
{
if(creditScore >= 600 || months > 12)
{
result = "Loan approved";
}
// missing else-statement
else {
result = "Loan denied";
}
}
...
您实际上并不需要其他级别的if子句,您可以写:
if (salary >= 40000 && (creditScore >= 600 || months > 12)) {
result = "Loan approved.";
}
else if (salary < 40000 && (creditScore >= 600 && months > 12)) {
result = "Loan approved.";
}
else {
result = "Loan denied.";
}
或
if (
(salary >= 40000 && (creditScore >= 600 || months > 12)) ||
(salary < 40000 && (creditScore >= 600 && months > 12))
){
result = "Loan approved.";
} else {
result = "Loan denied.";
}