如何将值从html文本框存储到javascript变量

时间:2022-04-04 14:01:47

I am creating a form which fetches the value from the database and stores the value dynamically into a table. I have a button named add which adds a row dynamically when a user press it and I have a hidden counter which counts the rows added.

我正在创建一个从数据库中获取值并将值动态存储到表中的表单。我有一个名为add的按钮,当用户按下它时,它会动态添加一行,我有一个隐藏的计数器,用于计算添加的行数。

The problem is, when I fetch the record, the counter shows the number of rows present and when I add a new row it goes back to 1.

问题是,当我获取记录时,计数器显示存在的行数,当我添加新行时,它返回到1。

If there are two rows in the table, the counter will show 2 but when I try to add a new row the counter shows 1.

如果表中有两行,则计数器将显示2但是当我尝试添加新行时,计数器显示1。

Is there any way to set the value from the counter text into the JavaScript variable and increment it?

有没有办法将计数器文本中的值设置为JavaScript变量并递增?

Here is my JavaScript code:

这是我的JavaScript代码:

<script language="javascript" type="text/javascript">
    var jj= 1;
    alert(jj);
    function addRow()
    {
        //alert(jj)
        var tbl = document.getElementById('zimtable');
        var lastRow = tbl.rows.length;
        var iteration = lastRow - 1;
        var row = tbl.insertRow(lastRow);

        var firstCell = row.insertCell(0);
        var el = document.createElement('input');
        el.type = 'text';
        el.name = 'zimname_' + jj;
        el.id = 'zimname_' + jj;
        el.size = 40;
        el.maxlength = 40;
        firstCell.appendChild(el);

        var secondCell = row.insertCell(1);
        var el2 = document.createElement('input');
        el2.type = 'text';
        el2.name = 'zimmob_' + jj;
        el2.id = 'zimmob_' + jj;
        el2.size = 13;
        el2.maxlength = 13;
        secondCell.appendChild(el2);

        // alert(i);
        //$('#hh').val(jj); 
        jj++;
        makhtab.hh.value=jj;

        alert(jj);
    }
</script>

Here is my php code to show the table:

这是我的PHP代码显示表:

<?php

    $zim = mysql_query("SELECT * FROM `makhzim` WHERE makhcode='$newsid' ORDER BY srno")or die(mysl_error());
    $ctrzim = 0;
    while ($zrow = mysql_fetch_array($zim)){
        $ctrzim++;
        print '<script type="text/javascript">';
        print "alert('$i')";
        print '</script>';
        echo "<tr>";
        echo "<td><input name='zimname_$ctrzim' type='text'  size='40' maxlength='20' value=$zrow[name] /></td>";
        echo "<td><input name='zimmob_$ctrzim'  type='text'   size='13' maxlength='20' value=$zrow[mobile] /></td>";
        echo "</tr>";
    }
    echo "</table>";
    echo "<input type=\"button\" value=\"Add\" onclick=\"addRow();\" /><input id=\"hh\" name=\"hh\" type=\"text\" value= '$ctrzim'/>";
?>

1 个解决方案

#1


0  

In your php code put the value of the counter to a hidden element

在您的PHP代码中,将计数器的值放入隐藏元素

<input type = "hidden"  id = "hiddenElementId" value = "your php counter value"/>

In your javascript

在你的JavaScript中

<script>
     var count;
     window.onload=function(){
         count =   =   document.getElementById("hiddenElementId").value;
     };
     function addRow() {
        alert(count);
        ..
        ..
        count++;
      }
<script>

#1


0  

In your php code put the value of the counter to a hidden element

在您的PHP代码中,将计数器的值放入隐藏元素

<input type = "hidden"  id = "hiddenElementId" value = "your php counter value"/>

In your javascript

在你的JavaScript中

<script>
     var count;
     window.onload=function(){
         count =   =   document.getElementById("hiddenElementId").value;
     };
     function addRow() {
        alert(count);
        ..
        ..
        count++;
      }
<script>