使用jQuery在我的嵌套条件语句中没有任何工作

Nothings working in my nested conditionals using jQuery

本文关键字:任何 工作 语句 条件 jQuery 我的 嵌套 使用      更新时间:2023-09-26

所以我现在正在学习jQuery,必须根据选择制作一个贷款计算器,并验证输入,然后输出结果。

我想确保你们知道我想做什么,所以我在这里有一个应该发生的事情的流程图:http://i59.tinypic.com/8z02sh.jpg

问题是我不知道怎么做这是Jquery。我早些时候问了一个问题,得到了一些反馈。没有一个代码的人提供彻底修复我的代码,实际上工作。我拿了一个叫"Parody"的家伙的作品,重写了我的代码。我不再犯错误了,我现在所做的对我来说是有意义的。问题是,当我输入一个数字,然后点击按钮,什么都不会发生。到目前为止,这是我的代码。

提前谢谢。

http://jsfiddle.net/mL74efjd/1/

HTML-

<h1>Loan Calc</h1>
<form id="salaryForm" name="salaryForm2" method="Post" action="javascript:void(0)">
<label for="salary">Enter your annual salary</label>
<input type="text" name="salary" id="salary">
</form>

<form id="creditform" name="creditForm" method="Post" action="javascript:void(0)">
<p>Please select your Credit Score</p>
<p><input type="radio" name="radio" id="over1" value="0">
<label for="over1">Over 600</label></p>
<p><input checked type="radio" name="radio" id="under1" value="0">
<label for="under1">Under 600</label></p>
</form>

<p> How long have you worked at your current job? </p>
<input class="job" id="job1" name="job" type="radio" value="0">
<label for="job1">I have worked at my current job over 1 year.</label><br>
<br/>
<input checked class="job" id="job2" name="job" type="radio" value="0">
<label for="job2">I have worked at my current job less than 1 year.</label><br>
</form>
<input type="button" id="check" name="check" value="Check">
<div id="message"></div>

JS-

$('#check').click(function()
{
  var salary;
  var isValid = $('#salaryForm').validate().form();
  // if validation passes, display a message
  if (isValid)
  {
    if (salary < 40000)
    {
      if ($('#over1').is(':checked'))
      {
        if ($('#job1').is(':checked'))
        {
          $('#message').html("Loan Approved.")
        }
        else {
          $('#message').html("Loan Denied.")
        }
      }
      else 
      {
        $('#message').html("Loan Denied.")
      }
    }
    else if (salary >= 40000)
    {
      if ($('#under1').is(':checked'))
      {
        if ($('#job2').is(':checked'))
        {
          $('#message').html("Loan denied.")
        }
        else 
        {
          if ($('#job1').is(':checked'))
            $('#message').html("Loan Approved.")
        }
      }else 
        {
          $('#message').html("Loan Approved.")
        }
      }
    }
});

// form validation
$.validator.setDefaults({
        errorElement: "span",
        errorClass: "form_error",
        errorPlacement: function(error, element){
            error.insertAfter(element)
        }
    });
    $.extend($.validator.messages,{
        required: "* Required field"
    });
$('#salaryForm').validate(
{
  rules:
  {
    salary:
    {
      required: true,
      digits: true,
      range: [1, 1000000]
    }
  }
});

可变薪酬没有在点击方法中初始化。看跌--

var salary=$("#salary").val();

此外,在fiddle中,jquery和jquery验证库不包括

我认为您还没有添加jQuery验证库。线路

 $('#salaryForm').validate().form();

将给出错误,因为validate()方法不存在。

因此,在html中添加验证库,并为salary变量赋值。

var salary = $("#salary").val();

那就行了。