繁体   English   中英

if / Else语句不在函数内部运行

[英]If/Else statement doesn't run inside function

我正在尝试验证一些输入字段。 更具体地说,该数字必须始终为正。

编辑:JS代码

$(document).ready(function($) {
  $('.error-message').hide();
  function priceCheck() {
    $('input[class="price"]').each(function() {
      priceValue = $(this).val();
      console.log(priceValue); //only runs until here and seems it exists the function then

      if (priceValue <= 0) {
        evt.preventDefault();
        return false;
      } else {

      }
    });
  }

  //POST FORM
  $("#offerInquiry").on('valid.fndtn.abide', function(evt) {
    //prevent the default behaviour for the submit event
    // Serialize standard form fields:
    var formData = $(this).serializeArray();
    var checked = $("#terms").is(":checked");
    priceCheck();


    if (checked == false) {
      $('.error-message-container').empty();
      $('.error-message-container').append("<%= pdo.translate("
        checkBox.isObligatory ") %>");
      $('.error-message').show();
      $('.bid-error').css("display", "block");
      evt.preventDefault();
      return false;
    } else {
      loading();
      $.post("/inquiry.do?action=offer&ajax=1", formData,
        function(data) {
          window.top.location.href = data.redirectPage;
        });
    }

    return false;
  });
});

我编写了一个函数,我单独调用表单提交。 但是它只运行到控制台日志。 为什么if else语句不执行?

您正在使用evt.preventDefault()但未在evt捕获事件。

例如,您可以尝试以下操作:将evt参数添加到priceCheck函数,然后在调用它时将evt传递给该函数,如下所示: priceCheck(evt)

但是,您无需在此处使用preventDefault 您可以简单地从priceCheck返回一个布尔值,并在您的submit处理程序中使用它。

您还遇到了字符串连接错误。 $('.error-message-container').append("<%= pdo.translate(" checkBox.isObligatory ") %>"); 缺少+来组合这些字符串。 您可以在JavaScript调试器的“控制台”选项卡中查看此类错误。 更新这是JSP注入,但是它可能无法在这里尝试使用它。服务器功能pdo.translate将仅在服务器端执行一次,并且不能通过客户端脚本调用...但是它可以发出客户端脚本。首先专注于解决其他问题,然后再回到这个问题上。)

最后,您正在读取字符串值并将它们与数字进行比较。 我使用parseFloat()input字段中的这些值转换为数字。

这是固定代码。

 $(document).ready(function($) { $('.error-message').hide(); function priceCheck() { var priceValid = true; // innocent until proven guilty $('input[class="price"]').each(function() { priceValue = parseFloat($(this).val()) || 0; if (priceValue <= 0) { priceValid = false; return false; } }); return priceValid; } $("form").on("submit", function() { $("#offerInquiry").trigger('valid.fndtn.abide'); }); //POST FORM $("#offerInquiry").on('valid.fndtn.abide', function(evt) { //prevent the default behaviour for the submit event // Serialize standard form fields: var formData = $(this).serializeArray(); var checked = $("#terms").is(":checked"); var priceValid = priceCheck(); if (priceValid) { $('.error-message').hide(); if (checked == false) { $('.error-message-container').empty(); $('.error-message-container').append("<%= pdo.translate(" + checkBox.isObligatory + ") %>"); $('.error-message').show(); $('.bid-error').css("display", "block"); return false; } else { loading(); $.post("/inquiry.do?action=offer&ajax=1", formData, function(data) { window.top.location.href = data.redirectPage; }); } } else { $('.error-message').show().text("PRICE IS NOT VALID"); } return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="offerInquiry"> Price 1 <input type="text" class="price" id="price1" value="0.00" /> <br/>Price 2 <input type="text" class="price" id="price1" value="0.00" /> <br/> <input type='submit' /> <div class="error-message">ERROR!</div> </form> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM