如何在jQuery每个函数中获取此数据?帮我解决这个问题

时间:2019-04-29 19:45:07

标签: jquery

 function check_businessHours() {
      for (var i = 1; i <= 7; i++) {
        var weekday = document.getElementById('weekday'+i);
        var opening_time = document.getElementById('opening_time'+i);
        var closing_time = document.getElementById('closing_time'+i);
        var close = document.getElementById('close'+i);
        var always = document.getElementById('always'+i);
        if (opening_time.value == "" && closing_time.value == "") {
          if (close.checked == true || always.checked == true) {
              $('#check_business').html('');
              $('#check_business').removeClass('bg-danger');
          } else {
            var value = weekday.value+' business hours required';

            console.log(value)
          }
        } else {
            $('#check_business').html('');
            $('#check_business').removeClass('bg-danger');
        }
      }
    }

它会像这样在控制台日志中返回

Saturday business hours required
Sunday business hours required
Monday business hours required
Wednesday business hours required
Thursday business hours required

我想在下面给出的div中返回,但是我不能这样做。我无法解决这个问题

<div id="check_business" class="check-input"></div>

有人可以帮助我解决此问题吗?

2 个答案:

答案 0 :(得分:0)

在循环开始时清空DIV,然后为每天需要几小时的行添加一行。

function check_businessHours() {
  for (var i = 1; i <= 7; i++) {
    $("check_business").empty().removeClass("bg-danger");
    var weekday = document.getElementById('weekday' + i);
    var opening_time = document.getElementById('opening_time' + i);
    var closing_time = document.getElementById('closing_time' + i);
    var close = document.getElementById('close' + i);
    var always = document.getElementById('always' + i);
    if (opening_time.value == "" && closing_time.value == "") {
      if (!(close.checked || always.checked)) {
        $("#check_business").append(`<div>${weekday.value} business hours required</div>`).addClass("bg-danger");

        console.log(value)
      }
    }
  }
}

答案 1 :(得分:0)

DIV清除必须在循环开始之前进行,否则它将仅包含最后一个元素。

function check_businessHours() 
{
    $("check_business").empty().removeClass("bg-danger");

    for (var i = 1; i <= 7; i++) 
    {
        var weekday = document.getElementById('weekday'+i);
        var opening_time = document.getElementById('opening_time'+i);
        var closing_time = document.getElementById('closing_time'+i);
        var close = document.getElementById('close'+i);
        var always = document.getElementById('always'+i);

        if (opening_time.value == "" && closing_time.value == "") 
        {
            if (!(close.checked || always.checked)) 
            {
                $("#check_business").append(`<div>${weekday.value} business hours required</div>`).addClass("bg-danger");
                console.log(value)
            }
        } 
    }
}