我遇到了泡菜。我正在编写一个简单的抵押计算器,它通过 ajax 将数据发送到 calc.php。
使用 jquery,我正在检查以确保用户在请求的字段中输入了适当的值;both purchase price and down payment should be numbers (working), down payment can't be greater than purchase price (working), when a term is selected, interest rate auto populates (working) and an error message appears if either term or interest rate 未填充(工作)。
当用户提交表单并且缺少任何必填项时,错误消息会显示建议...但是,当用户更正错误时,表单不会提交。
通过查看我的不太复杂的代码,您很快就会看到,我仍在学习 Jquery,并将感谢您提供的任何输入。
在此先感谢您的帮助。
HTML 表单:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="css/css.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"> </script>
<script type="text/javascript" src="js/js.js" charset="utf-8"></script>
</head>
<body>
<h1>Mortgage</h1>
<form action="calc.php" method="post" id="mortgage-form">
<p class="errorMessage" id="shouldBeNumber">*This value can only contain numbers</p>
<p id="pPrice"><span class="errorMessage" id="PPmustBeNumber">*</span>Purchase Price:
<input type="text" name="purchase-price" id="purchase-price" value="0"/>
<span class="errorMessage" id="purchasePriceError">Please enter a number value!</span>
</p>
<p id="dPayment"><span class="errorMessage" id="DPmustBeNumber">*</span>Down Payment:
<input type="text" name="down-payment" id="down-payment" value="0"/>
<span class="errorMessage" id="downPaymentError">Down payment value must be less than Purchase Price!</span>
</p>
<p id="term">
<select id="loan-term">
<option value="noValueSelected">-- Select a Term -- </option>
<option value="15yrs">15 Years</option>
<option value="20yrs">20 Years</option>
<option value="30yrs">30 Years</option>
</select><span class="errorMessage" id="termRequired"> * Term is required.</span>
</p>
<div id="interest-rate"></div>
<p><input type="submit" name="submit" id="submit" value="Calculate!" /></p>
</form>
<footer>
<p>
© Copyright by Ricardo
</p>
</footer>
</body>
</html>
这是我的 JQUERY
$(function() {
//HIDE errors
$('.errorMessage').hide();
//Do something when loan-term changes
$('#loan-term').change(function(){
var loanTerm = $(this).val();
var interestOption = '';
//alert(loanTerm);
if (loanTerm == '15yrs') {
interestOption += "<option value='15yrFixed'>15 Year Fixed: 2.88% </option>";
} else if (loanTerm == '20yrs') {
interestOption += "<option value='20yrFixed'>20 Year Fixed: 3.52%</option>";
} else if (loanTerm == '30yrs') {
interestOption += "<option value='30yrFixed'>30 Year Fixed: 4.2%</option>";
}
if (loanTerm != "noValueSelected") {
var interestRate =
"<select id='interest-rate'><option value='selectInterestRate'>-- Select an Interest Rate -- </option>" + interestOption + "</select><span class='errorMessage' id='interestRequired'> * This field is required.</span>";
$('#interest-rate').html(interestRate).show();
} else if (loanTerm == "noValueSelected") {
$('#interest-rate').hide();
}
});//END loan-term check
$('#mortgage-form').submit(function() {
var purchasePrice = $('#purchase-price').val();
var downPayment = $('#down-payment').val();
var loanTerm = $('#loan-term').val();
var interestRate = $('#interest-rate').val();
function containsNumber(val) {
return /^(\d|,)+$/.test(val)
}
if (containsNumber(purchasePrice) == true && containsNumber(downPayment) == true ) {
$('#PPmustBeNumber').hide();
$('#DPmustBeNumber').hide();
$('#shouldBeNumber').hide();
//alert(purchasePrice + downPayment);
//CHECK IF DOWNPAYMENT IS LESS THAN PURCHASE PRICE.
if (downPayment < purchasePrice) {
$('#downPaymentError').hide();
} else {
$('#downPaymentError').show();
}
} else if (containsNumber(purchasePrice) == false){
$('#PPmustBeNumber').show();
$('#shouldBeNumber').show();
} else if (containsNumber(downPayment) == false) {
$('#DPmustBeNumber').show();
$('#shouldBeNumber').show();
}
if (loanTerm != "noValueSelected") {
$('#termRequired').hide();
$("#interest-rate").change(function() {
if (interestRate != "selectInterestRate"){
$('#interestRequired').hide();
} else if (interestRate == "selectInterestRate"){
$('#interestRequired').show();
}
})
} else if (loanTerm == "noValueSelected"){
$('#termRequired').show();
}
if ( purchasePrice && downPayment && loanTerm && interestRate) {
var data = new Object();
data.purchasePrice = purchasePrice;
data.downPayment = downPayment;
data.loanTerm = loanTerm;
data.interestRate = interestRate;
data.interestOption = interestOption;
//create an object of Ajax options:
var options = new Object();
//Establish each setting:
options.data = data;
options.dataType= 'text';
options.type = 'get';
options.url = 'calc.php';
//perform the request:
$.ajax(options);
}
return false;
}); //END SUBMIT FUNCTION
});