0

我似乎无法检查输入字段是否既为空又为必需,如果是则提醒用户。我目前在不查看是否需要输入字段的情况下让它工作,问题是当非必填字段为空时会弹出警报。

这是代码:

    $('.close-and-show-next').click(function() {
      var empty = $(this).parent().find("input:visible").filter(function() {
      return this.value === "";
    });
    if(empty.length && empty.closest('span').hasClass('required')) {
      //At least one input is empty
      $(this).parent().parent().find('.numberlabel').css('background-color','#FF0000');
      alert('One or more fields have been left blank. Please press EDIT to correct before pressing continue.');
    }
    });

这是小提琴:http: //jsfiddle.net/joseph_a_garcia/uuVjy/1/

4

1 回答 1

0

跨度不是输入的父级,因此closest()不起作用,您应该使用siblings()

if (empty.length && empty.siblings('span.required').length) {...
于 2013-07-20T09:10:50.627 回答