-1

我正在使用这个 Javascript 在preq已经包含 4 行的表中添加行。问题是,当我使用此 Javascript 行添加行时,会在表单上正确添加行,但如果表单上出现一些错误,并且如果重新加载表单,则新添加的行不可见,我还希望行内输入的文本可见 在此处输入图像描述

javascript

<script type="text/javascript">
            var counter = 5;
            function addrow()
            {
                if (counter > 10)
                {
                    alert("Only 10 Kind of Works are allowed");
                return false;
                }
            $('#preq > tbody:last').append('<tr><td><input title="Enter Kind Of work" readonly="readonly" onclick="if(this.value!=\'\'){this.value=\'\';opendrop();}else{opendrop();}" id="other_work' + counter + '" name="other_work' + counter + '" type="text" size="30" onclick="opendrop()" <?php if (isset($errors)) { echo 'value="'.htmlentities(@$_POST['other_work' + counter + '']).'"'; } ?>></td><td><input name="client_name' + counter + '" type="text" id="client_name' + counter + '" size="40"/></td><td><input name="firm_name' + counter + '" type="text" id="firm_name' + counter + '" size="40"/></td></tr>');
                counter++;
            }
            </script>

html代码

<table align="left" id="preq" style="display: none;">
                <tr>
                                </tr>
                                    <tr>
                                    <th align="left"><label id="Label1"> Kind of Work </label></th>
                                    <th align="left"><label id="Label1"> Name Of The Client </label></th>
                                    <th align="left"><label id="Label1">Name of Firm / Organisation </label></th>
                                    </tr>
            <tbody>

            <!--4 rows alredy present here-->
            </tbody>
    <tr><td colspan="3"><input type="button" class="button" value="Add Kind of Work" onclick="addrow()"/></td></tr>

            </table>
4

2 回答 2

1

如果您希望新添加的行即使在重新加载表单后仍然存在,那么您需要将其保存到数据库中,否则每次重新加载页面时数据都会丢失。

于 2013-01-05T11:09:38.393 回答
0

尝试 add else,因此,只有在没有错误时才添加行(在 上发出警报if())。

if (counter > 10) {
  alert("Only 10 Kind of Works are allowed");
  return false;
} else {
  $('#preq > tbody:last').append('<tr><td><input title="Enter Kind Of work" readonly="readonly" onclick="if(this.value!=\'\'){this.value=\'\';opendrop();}else{opendrop();}" id="other_work' + counter + '" name="other_work' + counter + '" type="text" size="30" onclick="opendrop()" <?php if (isset($errors)) { echo 'value="'.htmlentities(@$_POST['other_work' + counter + '']).'"'; } ?>></td><td><input name="client_name' + counter + '" type="text" id="client_name' + counter + '" size="40"/></td><td><input name="firm_name' + counter + '" type="text" id="firm_name' + counter + '" size="40"/></td></tr>');
  counter++;
  return true;
}
于 2013-01-05T09:46:59.427 回答