如何使用javascript更改隐藏类型输入字段的值

How to change the value of a hidden type input field with javascript

本文关键字:输入 字段 类型 隐藏 何使用 javascript      更新时间:2023-09-26

我想做的是,每次用户单击按钮Add并调用addRow函数时,我也会调用row_counter函数,将隐藏输入类型字段的值设置为已创建的动态行数。因此,如果我动态地创建了3行,那么隐藏的输入类型字段必须是3。我是javascript新手,所以请耐心等待。

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {
    $product_name = mysql_real_escape_string($_POST['product_name']);
    $price = mysql_real_escape_string($_POST['counter']);
    $details = mysql_real_escape_string($_POST['details']); 
    $category = mysql_real_escape_string($_POST['category_choice']);
    $condition= mysql_real_escape_string($_POST['condition']);;
    $supplier_choice= mysql_real_escape_string($_POST['supplier_choice']);;
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = mysql_query("INSERT INTO products (product_name, price, details, category, active, supplier, date_added) 
        VALUES('$product_name','$price','$details','$category','$condition','$supplier_choice',now())") or die (mysql_error());

     $pid = mysql_insert_id();
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['my_photo']['tmp_name'], "../inventory_images/$newname");
    //ean den yperxan oi 2 epomenes grammes ean ekana refresh tha prospathouse na perasei to idio proion ksana!!
    header("location: inventory_list.php"); 
    exit();
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inventory Area</title>
<link rel="stylesheet" href="../style/style.css" type="text/css" media="screen"/>
<SCRIPT language="javascript">
    //Add Information
    i=1;
    function addRow(tableID,counter1) {
        //The rows num starts from 0
        var table = document.getElementById(tableID);
        var rowCount = 7;
        var row = table.insertRow(rowCount);
         var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var element = document.createElement("input");
        element.type = "text";
        element.name = "information_" + i;
        element.id = "information_" + i;
        element.size = 84;
        cell2.appendChild(element);
        i++;
        row_counter_inc(counter1);
    }
    function deleteRow(tableID) {
        if(i>=2){
            var table = document.getElementById(tableID);
            table.deleteRow(i+5);
            i--;
        }
    }
    function row_counter_inc(counter2){
        w=i;
        w=w-1
        document.getElementByID("counter2").value=w;
    }
</SCRIPT>

<div align="center" id="mainWrapper"> 
  <?php include_once("template_header.php");?>
  <div id="pageContent">
        <div align="left" style="margin-left:24px">
            <h2>Inventory List</h2>
            <?php echo $productList ?>
        </div>
  </div>
  <div>
        <form action="inventory_list.php" enctype="multipart/form-data" name="my_Form" id="my_Form" method="post">
        <a name="add_New_Product_Form" id="add_New_Product_Form">
            <h2>Add New Product Form</h2>
        </a>
        <table width="80%" border="0" cellspacing="0" cellpadding="6" id="dataTable">
            <tr>
                <td>Όνομα</td>
                <td>
                    <label>
                        <input name="product_name" type="text" id="product_name" size="80" />

                    </label>
                </td>
            </tr>
            <tr>
                <td>Κατάσταση</td>
                <td>
                    <label>
                        <input type="radio" name="condition" value="1"> Ενεργό</input>
                        <input type="radio" name="condition" value="0"> Ανενεργό</input>
                    </label>
                </td>
            </tr>

            <tr>
                <td>Τιμή</td>
                <td>
                    <label>
                        <input name="price" type="text" id="price" size="20" />
                    </label>
                </td>
            </tr>
            <tr>
                <td>Προμηθευτές</td>
                <td>
                    <?php echo $suppliers_list; ?>
                </td>
            </tr>
            <tr>
                <td>Κατηγορία</td>
                <td>
                    <?php echo $category_list; ?>
                </td>
            </tr>
            <tr>
                <td>Περιγραφή</td>
                <td>
                    <label>
                        <textarea name="details" id="details" cols="64" rows="5"></textarea>
                    </label>
                </td>
            </tr>
            <tr>
                <td>Τεχνικά Χαρακτηριστικά</td>
                <td>
                    <input type="text" name="information_0" id="information_0" size="84"/>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="hidden" name="counter" id="counter"/>
                    <INPUT type="button" value="Προσθήκη" onClick="addRow('dataTable','counter')"/>
                    <INPUT type="button" value="Αφαίρεση" onClick="deleteRow('dataTable')"/>
                </td>
            </tr>
            <tr>
                <td>Φωτογραφία</td>
                <td>
                    <label>
                        <input type="file" name="my_photo" id="my_photo"/>
                    </label>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <label>
                        <input type="submit" name="button" id="button" value="Αποθήκευση" />
                    </label>
                </td>
            </tr>
        </table>
        </form>

  </div>
   <br /><br />
  <?php include_once("../template_footer.php");?>
</div>

enter code here

这应该适用于您:

 document.getElementById("counter").value = 3;