duan47676379 2016-04-19 11:29
浏览 77
已采纳

检查表单中的多个div,如果div没有指定的类,则执行警报并阻止提交

OK, I have a multiple divs dynamically created in php depending on a variable and I would like to check for each of them if it has a certain class. When I upload an image which will then append the image in the <div id="img-pick">, and add a class allocated. I would like to check if one or any of the divs does not have the allocated class then do an alert and stop the form from being submitted when clicked on the submit button unless all the <div id="img-pick"> have the class allocated to them.

This is my code for the form:

<form action="" method="POST" id="image-form">
<div class="row options-heading"><h3>UPLOAD <?php echo $loopvalue; ?> IMAGES</h3></div>
<div class="row options-content">
  <div class="grid-wrapper <?php echo $model; ?> <?php echo $option_style; ?> ui-sortable clearfix">
    <?php for ($i = 1; $i <= $loopvalue; $i++)  : ?>
    <div class="<?php echo $col_setting; ?> grid-item">
        <div id="img-pick" class="<?php echo $option_shape; ?>">
            <a href="#" class="add tooltip" data-tip="Click to Upload"></a>
            <div class="tools">
                <a href="#" class="del tooltip" data-tip="Remove image?"></a>
            </div>
            <a href="#" class="reset tooltip" data-tip="Upload stalled? Click to reset"></a>
        </div>
    </div>
    <?php endfor; ?>
  </div>
</div>
<div class="row options-nav-btn">
  <div class="col-md-4"></div>
  <div class="col-md-4"></div>
  <div class="col-md-4"><input type="submit" class="btn btn-success pull-right" value="Continue" id="collectImages"></div>
</div>
</form>

And this is the javascript:

$('#collectImages').click(function()
  if(!$('#img-pick').hasClass("allocated"))
  {
    sweetAlert("Oops...", "You haven't finished picking your images", "error");
    return false;
  }
});

With this script, the alert and the prevent of the submit works only when none of the <div id="img-pick"> has the allocated class, but if one or more, but not all of them, has the allocated class then the alert doesn't seem to work and the form gets submitted anyway...

  • 写回答

1条回答 默认 最新

  • dousong2967 2016-04-19 11:32
    关注

    Use a loop:

    $('#collectImages').click(function(e) {
    e.preventDefault();
    var valid = true;
    $('#img-pick').each(function(){
       if(!$(this).hasClass("allocated"))
      { 
       valid = false;
      }
    });
     if (!valid) {
     sweetAlert("Oops...", "You haven't finished picking your images", "error");
     } else {$(this).closest('form').submit();}
    });
    

    or:

    $('#collectImages').click(function()
      if($('#img-pick').length != $("#img-pick.allocated").length)
      {
        sweetAlert("Oops...", "You haven't finished picking your images", "error");
        return false;
      }
    });
    

    Note: change the id with a class

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址