首页 > 解决方案 > JavaScript 返回 false 但仍在提交

问题描述

这是HTML:

<form method="post" action="http://localhost:8080/center/add" onsubmit="return validate()">
  <div class="col-lg-12">
    <table id="myTable">
      <thead>
        <tr>
          <td>
            Start Time
          </td>
          <td>
            End Time
          </td>
          <td>
          </td>
        </tr>
      </thead>
      <tbody>
        <tr class="t-row" title="" style="background: rgb(255, 255, 255);">
          <td>
            <input type="text" onfocus="clearError(this)" class="form-control time-box-width m-t-10px vTimeStart" name="vTimeStart[]" placeholder="Please enter start time in 24 hrs format">
          </td>
          <td>
            <input type="text" onfocus="clearError(this)" class="form-control time-box-width m-t-10px vTimeEnd" name="vTimeEnd[]" placeholder="Please enter end time in 24 hrs format">
          </td>
          <td>
            <input type="button" class="days-btn  m-t-10px" value="Delete">
          </td>
        </tr>
        <tr class="t-row" title="" style="background: rgb(255, 255, 255);">
          <td><input type="text" name="vTimeStart[]" placeholder="Please enter start time in 24 hrs format" onfocus="clearError(this)" class="vTimeStart form-control time-box-width m-t-10px"></td>
          <td><input onfocus="clearError(this)" type="text" name="vTimeEnd[]" placeholder="Please enter end time in 24 hrs format" value="" class="vTimeEnd form-control time-box-width m-t-10px"></td>
          <td><input type="button" value="Delete" class="days-btn  m-t-10px"></td>
        </tr>
      </tbody>
    </table>

    <div class="divide-div"></div>

  </div>
  <div class="modal-footer m-footer" style="border-top: 0;">
    <input class=" days-btn m-t-10px" id="submit" type="submit" value="Submit">
    <input class=" days-btn m-t-10px" id="close-popup" data-dismiss="modal" type="button" value="Close">
  </div>
</form>

JavaScript:

function validate() {

        $('.t-row').each(function(i, obj) {

          var currentStartTimeValue = $('#myTable .vTimeStart').eq(i).val();
          var currentEndTimeValue = $('#myTable .vTimeEnd').eq(i).val();

          if(currentStartTimeValue != '') {
            var v = valdateFormat(currentStartTimeValue);
            alert(v);
            if(!v) {
              alert('Please enter value in HH:MM format.');
              return false;
            }
          }

          if(currentEndTimeValue != '') {
            var v = valdateFormat(currentEndTimeValue);
            if(!v) {
              alert('Please enter value in HH:MM format.');
              return false;
            }
          }

          if(i > 0){
            var previousIndex = i - 1;
            var lastEndTimeValue = $('#myTable .vTimeEnd').eq(previousIndex).val();
            alert(currentStartTimeValue);
            var v = valdateFormat(currentStartTimeValue);
            if(currentStartTimeValue < lastEndTimeValue){
              $(this).attr('title','Current StartTime must be lesser than current EndTime!');
              alert('Current StartTime cannot be lesser than previous EndTime');
              return false;
            }
          }

          if(!currentStartTimeValue){
            var v = valdateFormat(currentStartTimeValue);
            if(!v) {
              alert('Please enter value in HH:MM format.');
              return false;
            }
            $(this).attr('title','Enter value for Start Time!');
            alert('Enter value for Start Time!');
            return false;
          } else if(!currentEndTimeValue){
            var v = valdateFormat(currentEndTimeValue);
            if(!v) {
              alert('Please enter value in HH:MM format.');
              return false;
            }
            $(this).attr('title','Enter value for End Time!');
            alert('Enter value for End Time!');
            return false;
          } else if(currentStartTimeValue >= currentEndTimeValue){
            $(this).attr('title','Current StartTime must be lesser than current EndTime!');
            alert('Current StartTime must be lesser than current EndTime');
            return false;
          }

          if(i === $('.t-row').length - 1) {
            // alert('All good!');
          }

        });

        // return true;

      }

我的表单是动态添加tr的,只有一个按钮add row to bottom。当我单击Submit按钮并且验证失败时,它仍然提交表单。

标签: javascriptcodeigniter-3

解决方案


您的validate函数不返回任何内容。

这些return语句属于传递给的匿名函数.each


推荐阅读