如何从HTML显示的复选框上输入所选值

问题描述:

  • 我希望能够摆脱我所选择的复选框值
  • 的值将是0为第2行等
  • 第一行1
  • 该代码工作并显示所有内容,但它将所有值显示为 0表示它不增加它。

============================================ ============================如何从HTML显示的复选框上输入所选值

<html> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
    <button type='submit' name='submit' id='buttonParent'>Submit</button> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script> 

    <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.min.js" type="text/javascript"></script> 
    <script> 
    $(document).ready(function() { 
     /* Get the checkboxes values based on the class attached to each check box */ 
     $("#buttonClass").click(function() { 
      getValueUsingClass(); 
     }); 

     /* Get the checkboxes values based on the parent div id */ 
     $("#buttonParent").click(function() { 
      getValueUsingParentTag(); 
     }); 
    }); 

    function getValueUsingParentTag(){ 
     var chkArray = []; 

     /* look for all checkboes that have a parent id called 'checkboxlist' attached to it and check if it was checked */ 
     $("#checkboxlist input:checked").each(function() { 
      chkArray.push($(this).val()); 
     }); 

     /* we join the array separated by the comma */ 
     var selected; 
     selected = chkArray.join(',') ; 

     /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */ 
     if(selected.length > 1){ 
      alert("You have selected " + selected); 
     }else{ 
      alert("Please at least one of the checkbox"); 
     } 
    } 
    </script> 
    <?php 
    session_start(); 
    require_once('../mysql_connect.php'); 

        $query = "select * from inventory"; 
        $res = mysqli_query($dbc, $query); 

        echo 
         '<br><br><br><br><br> 
         <div class = "Table"> 
          <table border = "2pt solid black" align = "left" cellpadding = "2px" 
           bordercolor = black> 
          <tr> 
          <td width = 7%"> 
           <div align = "left"><b>CHECK.</div></b> 
          </td> 
           <td width = "7%"> 
           <div align = "left"><b>INVENTORY NAME</div></b> 
          </td> 
          <td width = "3%"> 
           <div align = "left"><b>CATEGORY</div></b> 
          </td> 

          <td width = "3%"> 
           <div align = "left"><b>Quantity</div></b> 
          </td> 

          <td width = "7%"> 
           <div align = "left"><b>MEASUREMENT</div></b> 
          </td> 
          <td width = "7%"> 
           <div align = "left"><b>SPOILAGE DATE</div></b> 
          </td> 
          </tr>'; 
          $num_rows = mysqli_num_rows($res); 
      for($i=0;$i<$num_rows;$i++) 
      { 
        while($fetch = mysqli_fetch_array($res, MYSQL_ASSOC)) 
        { 



         echo "<tr> 
           <td width=\"7%\"> 
            <div id='checkboxlist' align=\"left\"><input type='checkbox' class='chk' value='$i'/></div> 
           </td> 
           <td width=\"7%\"> 
            <div align=\"left\">{$fetch['inventory_name']}</div> 
           </td> 
           <td width=\"3%\"> 
            <div align=\"left\">{$fetch['category']}</div> 
           </td> 
           <td width=\"3%\"> 
            <div align=\"left\"><input type = 'number' placeholder = {$fetch['quantity']} type = 'number' name = 'qty[]' id='qty' class = 'qty'/></div> 
           </td> 
           <td width=\"7%\"> 
            <div align=\"left\"><input type='text' name='measure' class='measure' id='measure'/></div> 
           </td> 
           <td width=\"7%\"> 
            <div align=\"left\">{$fetch['spoilage_date']}</div> 
           </td 
          </tr>"; 
         }      
      } 

        echo '</table></div>'; 
    ?> 
    </form> 



    </html> 

你在for的时候是你的原因。根本不需要for循环。在此之前设置$i = 0,并在里面增加它。

+0

非常感谢你,我应该已经意识到你并不需要激励数据 –

+0

我的nenxt问题是如何查询这些结果以及数量输入和度量输入? –