javascript - 如何限制添加字段?

标签 javascript limit field

如果有人愿意帮助我,我将非常感激

如何限制以下 html 代码中列的添加,比如说最多只能添加 5 列?

  <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="friends"
      <table width="100%" cellpadding="10" cellspacing="0" id="my_friends">
        <tr>
          <th colspan="2" style="text-align: center;"><?php echo $text_enter_friend; ?></th>
        </tr>
        <tr>
          <td class="left" width="30%"><span class="required">*</span> <?php echo $entry_friend; ?></td>
          <td class="left"><input type="text" name="friend" value="<?php echo $friend; ?>" size="30" maxlength="45" />
            <?php if ($error_friend) { ?>
            <span class="error"><?php echo $error_friend; ?></span>
            <?php } ?></td>
        </tr>
        <?php if ($friends) { ?>
        <?php foreach ($friends as $result) { ?>
        <tr>
          <td style="vertical-align: top;"><?php echo $entry_friend; ?></td>
          <td style="vertical-align: top;"><input type="text" name="friends[]" value="<?php echo $result; ?>" size="30" maxlength="45" /></td>
        </tr>
        <?php } ?>
        <?php } ?>
      </table>
      <table width="100%" cellpadding="10" cellspacing="0">
        <tr>
          <td></td>
          <td class="right"><a onclick="addFriend();" class="button"><span><?php echo $button_add_friend; ?></span></a><a onclick="removeFriend();" class="button"><span><?php echo $button_remove; ?></span></a></td>
        </tr>
        <tr>
          <td colspan="2" class="center">
            <p><?php echo $text_message; ?></p>
            <p><a onclick="$('#friends').submit();" class="button"><span><?php echo $button_submit; ?></span></a></p>
            <p><?php echo $text_addresses; ?></p>
          </td>
        </tr>
      </table>
    </div>
  </form>

<script type="text/javascript"><!--
function addFriend() {
    var tbl = document.getElementById('my_friends');
    var iteration = tbl.tBodies[0].rows.length;
    newRow = tbl.tBodies[0].insertRow(-1);
    var newCell = newRow.insertCell(0);
    newCell.innerHTML = '<?php echo $entry_friend; ?>';
    var newCell1 = newRow.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'friends[]';
    el.size = 30;
    el.maxlength = 45;
    newCell1.appendChild(el);
//  if (newCell > 2) tbl.addCell(newCell + 1);
}

function removeFriend() {
    var tbl = document.getElementById('my_friends');
    var lastRow = tbl.rows.length;
    if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
//--></script>

提前致谢

最佳答案

表#my_friends 默认有 2 行。所以你必须计算行数。

function addFriend() {
var tbl = document.getElementById('my_friends');
var iteration = tbl.tBodies[0].rows.length;
if(iteration>=7)return;//leave the function if already 5 rows have been added

//rest of the function

关于javascript - 如何限制添加字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4059639/

相关文章:

python - 对表单更改使用react

mysql - 如何将 MySql 表导出/转储到文本文件中,包括字段名称(也称为标题或列名称)

javascript - 页面加载时 javascript 的语法

javascript - 如何捕获来自另一个域的 iframe 的点击?

android - 如何使用内容提供者添加限制条款

c# - 既然可以直接访问成员变量,为什么还要使用私有(private)属性 setter 呢?

javascript - 如何在循环中处理异步 Node.js

javascript - 无法使用 jQuery 更改 div 的值

javascript - 限制jstree中子节点的选择数量

mysql - 具有连接和分页功能的 eclipselink