在提交

时间:2016-05-23 15:43:35

标签: javascript jquery checkbox

好吧,我有一个表单,用户可以选择带有复选框的项目,用户输入他想用输入文本发送的项目数,但有些项目需要一个复选框所包含的序列号。

这是我的方法:

<script>
      function checkall()
      {
        var ok=true;
        $('input:checkbox[id^="art"]:checked').each(function() {
          var vals = $(this).val();
          //first attempt
          if (typeof document.getElementById(vals+"existencia") === 'undefined') {
            ok=false;
            return false;
            alert("Error message");
          }else
          if (typeof document.getElementById(vals+"-serie")==='undefined') {
            ok=false
            return false;
            alert("Error message 2");
          }

//second attempt
          if (obj.hasOwnProperty(vals+'existencia')) {
            art = document.getElementById(vals+"existencia").value;
            alert(art);
          }else if (obj.hasOwnProperty(vals+'serial')) {
            sert=document.getElementById(vals+"-serie").value;
            alert(sert);
          }
        });


        if(ok)
        {
          document.getElementById("myform").submit();
        }
      }
      </script>

该脚本用于循环所有选中的复选框,如果输入为空,则停止并提醒用户,另一个阶段是当他检查带有序列号的文章时,至少有1个必须检查并提醒客户

e1

第一次尝试,我试过,因为这些元素被定义,第二次尝试抛出obj is not defined,也许我很想念我搜索的代码和答案,有人可以帮助实现我的目标或解释我做了什么错了,谢谢。

更新:

if ($inicio == 0) {
                    echo "<tr bgcolor= '#f4f4f4' >";
                    $inicio = 1;
                  } else {
                    echo "<tr bgcolor= white>";
                    $inicio = 0;
                  }
                  ?>
                  <td style="width: 10%;text-align: center;">
                    <input type="checkbox" name="art[]" id="art" value="<?php echo $articulo; ?>" /></td>
                    <td width="12%" align="center"><?php echo $diferencia_dias ?> d&iacute;as</td>
                    <td width="8%" style="text-align: center;"><?php echo $articulo; ?></td>

                    <td width="45%"><?php echo $descripcion; ?></td>
                    <?php
                    if($numserie>0){
                      $esto = trim($articulo);
                      $bus_serie = mssql_query("execute serial_madness '$esto','$almacen'");
                      echo "<td style='width:10%;text-align:left;'>";
                      while($res_serie = mssql_fetch_array($bus_serie))
                      {
                        echo '<input type="checkbox" id="'.$articulo.'-serie" name="'.$articulo.'-Serie[]" value="'.$res_serie["ARTICULO"].','.$res_serie["NUMSERIE"].'_'.$valor.'  "> '.$res_serie["NUMSERIE"].' <br>';
                      }
                      echo "</td>";
                    }else{
                      ?><td style="width:10%;text-align:center;"><input type="number" style="text-align:center;width:30px; height:30px;" id="<?php echo $articulo_c; ?>existencia" name="<?php echo
                      $articulo_c; ?>existencia" value="<?php echo (int)$existencias; ?>" min="1" max="<?php echo (int)$existencias; ?>"/></td>
                      <?php
                    } ?>

1 个答案:

答案 0 :(得分:0)

因此,为了检查复选框,您只需使用jquery。

示例:switch (frameType) { case 49: // Clearly frametype won't be 49 so this won't happen case 50: // Same problem

上面的示例将选中ID为“checkboxId”的复选框,将$("#checkboxId").prop('checked', true);更改为true将取消选中此框;

如果要检查检查了多少或哪些输入,可以使用jquery选择器返回的数组的.length。

以下将检查是否所有带有以“test”开头的ID的复选框都被选中:

false

同样,如果长度等于零,您可以检查是否有任何内容。

我准备了一个简单的例子,我想解决你在这里解决的一般问题。

https://plnkr.co/edit/2v6x1DXRpAiaKiBFSMz6?p=preview

请注意,当您在我的plunker示例中单击“提交”时,将自动选中所有复选框。

如果单击“验证”按钮,它将验证是否确实已选中所有复选框。

您应该能够使用我的示例为您的特定案例创建解决方案。

您可能还想使用if($('[id^="test"]:checkbox').length == $('[id^="test"]:checkbox:checked').length) { // Do stuff here } jquery函数  例: .is()

有关类似的解决方案和解释,请参阅以下答案:Check if checkbox is checked with jQuery

相关问题