将动态文本框中的值存储到javascript变量中

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

hi i am new to php i hav a form which adds a dynamic table row on a button click i am being able to save it aswell as to retrieve it but the problem comes when i retireve the dynamic row with its data i've created a hidden textbox which stores the counter of rows added but the problem is when i try to store its value into my javascript variable it shows NaN whitout the counter value being stored i cant save the value into my database i am just a newbie so please help me out here is my javascript code

嗨,我是php的新手,我有一个表单,在按钮上添加动态表格行,我可以保存它以及检索它,但问题来自我退出动态行的数据我创建了一个隐藏文本框,存储添加的行的计数器,但问题是,当我尝试将其值存储到我的javascript变量时,它显示NaN whitout存储的计数器值我不能将值保存到我的数据库中我只是一个新手所以请帮助我这里是我的javascript代码

<script language="javascript" type="text/javascript">
                        var jj= document.getElementByName('hh').value;
                        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 which selects the data n shows the output

这是我的PHP代码,它选择数据n显示输出

                    $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++;
                                       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='hidden' value='$ctrzim'/>";
                            ?>

2 个解决方案

#1


0  

Try using document.getelementbyid

尝试使用document.getelementbyid

var hiddenvalue = document.getElementById('hh').value;

#2


0  

try changing:

var jj = document.getElementByName('hh').value;

to:

var jj = document.getElementsByName('hh')[0].value;

Please note: the function's name is getElementsByName(). Note the plural "getElements".

请注意:函数的名称是getElementsByName()。注意复数“getElements”。

Updated jsFiddle: http://jsfiddle.net/aazev/Ct76z/

更新了jsFiddle:http://jsfiddle.net/aazev/Ct76z/

PHP code shown for illustration purposes only.

PHP代码仅用于说明目的。

#1


0  

Try using document.getelementbyid

尝试使用document.getelementbyid

var hiddenvalue = document.getElementById('hh').value;

#2


0  

try changing:

var jj = document.getElementByName('hh').value;

to:

var jj = document.getElementsByName('hh')[0].value;

Please note: the function's name is getElementsByName(). Note the plural "getElements".

请注意:函数的名称是getElementsByName()。注意复数“getElements”。

Updated jsFiddle: http://jsfiddle.net/aazev/Ct76z/

更新了jsFiddle:http://jsfiddle.net/aazev/Ct76z/

PHP code shown for illustration purposes only.

PHP代码仅用于说明目的。