【问题标题】:Navigate form fields with arrow keys使用箭头键导航表单域
【发布时间】:2016-04-05 02:31:04
【问题描述】:

我有一个带有 New Row 函数脚本的表单。然后我添加了一个新脚本 navigate form fields 从这个站点Form Navigation script

导航脚本在第 1 行可以正常工作。我可以使用箭头键在字段之间移动(从右到左,从左到右)。如果我添加新行,我可以移动到第 2 行(从上到下),但我不能返回到 第 1 行从下到上) 并且我不能在 第 2 行 中的字段之间移动(从右到左,从左到右)。如果我再次添加新行,Row 3 navigate script 根本不起作用,我只能从 Row 1 移动到 第 2 行(从上到下),第 2 行第 3 行不起作用。

这是我的代码,New Row脚本:

var row_id = 1;
function addRow(tableID) {            
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    row.id = 'id' + row_id;
    row_id++;
    for(var i=0; i<colCount; i++) {
       var newcell = row.insertCell(i);
       newcell.innerHTML = table.rows[0].cells[i].innerHTML;
       //alert(newcell.childNodes);
       switch(newcell.childNodes[0].type) {
             case "text":
                  newcell.childNodes[0].value = "";
             break;
             case "checkbox":
                  newcell.childNodes[0].checked = false;
             break;
             case "select-one":
                  newcell.childNodes[0].selectedIndex = 0;
             break;
             case "select":
                  newcell.childNodes[0].selectedIndex = 0;
             break;
             default:
                  newcell.childNodes[0].value = "";
             break;
        }               
    }
}

表格代码:

<div class="row">
  <div class="col-md-12">
    <div class="table-responsive">
      <table class="table table-bordered table-hover table-striped sticky-header">
[....]
<tbody id="dataTable2">
   <?php if(!isset($dtdetail)) {;
         if(isset($message)) {

         for ($i=0; $i < $jmldtl; $i++) { ?>

         <tr>                                                                                                
            <td>
               <input type="text" name="tp_sal[]" id="tp_sal" size="5" value="<?php echo set_value('tp_sal['.$i.']'); ?>"/></td>
            <td>
               <input type="text" name="r_tpc_1[]" id="r_tpc_1" size="5" value="<?php echo set_value('r_tpc_1['.$i.']'); ?>"/></td>
            <td>
               <input type="text" name="r_tpc_2[]" id="r_tpc_2" size="10" value="<?php echo set_value('r_tpc_2['.$i.']'); ?>"/></td>
            <td>
                <input type="text" name="r_tpc_3[]" id="r_tpc_3" size="15" value="<?php echo set_value('r_tpc_3['.$i.']'); ?>"/></td>  
            <td>
                <input type="text" name="r_etr_1[]" id="r_etr_1" size="13" value="<?php echo set_value('r_etr_1['.$i.']'); ?>"/></td>                                                  
        </tr>
        <?php } } else { ?>
        <tr>                                                                                               
           <td>
              <input type="text" name="tp_sal[]" id="tp_sal" size="5" value="<?php $a=set_value('tp_sal'); echo $a; ?>"/></td>
           <td>
              <input type="text" name="r_tpc_1[]" id="r_tpc_1" size="5" value="<?php $a=set_value('r_tpc_1'); echo $a; ?>"/></td>
           <td>
              <input type="text" name="r_tpc_2[]" id="r_tpc_2" size="10" value="<?php $a=set_value('r_tpc_2'); echo $a; ?>"/></td>
           <td>
              <input type="text" name="r_tpc_3[]" id="r_tpc_3" size="15" value="<?php $a=set_value('r_tpc_3'); echo $a; ?>"/></td>  
           <td>
              <input type="text" name="r_etr_1[]" id="r_etr_1" size="13" value="<?php $a=set_value('r_etr_1'); echo $a; ?>"/></td>                                                  
       </tr>
 <?php } } else {
     foreach($dtdetail as $detail) { ?>
       <tr>                                                    
          <td>
             <input type="text" name="tp_sal[]" id="tp_sal" size="5" value="<?php echo $detail->tp_sal; ?>"/></td>
          <td>
             <input type="text" name="r_tpc_1[]" id="r_tpc_1" size="5" value="<?php echo $detail->r_tpc_1; ?>"/></td>
          <td>
             <input type="text" name="r_tpc_2[]" id="r_tpc_2" size="10" value="<?php echo $detail->r_tpc_2; ?>"/></td>
          <td>
             <input type="text" name="r_tpc_3[]" id="r_tpc_3" size="15" value="<?php echo $detail->r_tpc_3; ?>"/></td>  
          <td>
             <input type="text" name="r_etr_1[]" id="r_etr_1" size="13" value="<?php echo $detail->r_etr_1; ?>"/></td>                                               
      </tr>
<?php } } ?>
</tbody>
<tfoot class="bg-primary">
    <tr>
       <td colspan="31" align="center">
           <?php if(!isset($dtdetail)) {?>
                 <button type="button" class="btn btn-sm btn-info" onClick="addRow('dataTable2')">New Row</button>
           <?php } else { ?>
                 <button type="button" class="btn btn-sm btn-info" onClick="addRow('dataTable2')">New Row</button>
           <?php } ?>
       </td>
   </tr>
</tfoot>
[.....]

<?php
$this->load->view('template/js2');
?>

js2 文件中的 Form Navigation 脚本:

<script type="text/javascript">
  /*!
* formNavigation
* Copyright 2013 Ole Bjørn Michelsen <http://ole.michelsen.dk/>
* MIT license
*/
(function ($) {
    $.fn.formNavigation = function () {
        $(this).each(function () {
            $(this).find('input').on('keyup', function(e) {
                switch (e.which) {
                    case 39:
                        $(this).closest('td').next().find('input').focus(); break;
                    case 37:
                        $(this).closest('td').prev().find('input').focus(); break;
                    case 40:
                        $(this).closest('tr').next().children().eq($(this).closest('td').index()).find('input').focus(); break;
                    case 38:
                        $(this).closest('tr').prev().children().eq($(this).closest('td').index()).find('input').focus(); break;
                }
            });
        });
    };
})(jQuery);
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $('.table').formNavigation();
    });
</script>

【问题讨论】:

  • 如果用户想在文本框中的字符串内移动怎么办?从可访问性的角度来看,我担心这是糟糕的设计。
  • 嗯,请给点建议,我应该做什么样的设计?哦,顺便说一句,如果你知道如何让我的代码工作,请告诉我答案。

标签: javascript html forms codeigniter input


【解决方案1】:

问题

发生这种情况是因为导航脚本仅适用于在运行时表中的行。因此,表单导航脚本在文档就绪事件上运行:

$(document).ready(function () {
    $('.table').formNavigation();
});

此事件在页面的 HTML 结构完全加载时发生,即 addRow() 函数被调用之前。当调用 addRow() 函数时,它会添加一个新行,但这个新行没有应用表单导航事件处理程序。

解决方案

要解决此问题,请将 $(row).formNavigation(); 添加到 addRow() 函数的末尾。

【讨论】:

  • 非常感谢您的解释,脚本已经在您的解决方案中运行。
  • 嗨@jon-anderson 我有一个新的类似问题,仍然遇到addRow() 函数的问题,我希望你能帮助我。我创建了一个新脚本getWarning(),该脚本用于文本字段背景颜色验证,getWarning function。该规范仅适用于 Row 1,如果我在 Row 1 中填写规范编号为 1 的数据,则脚本正确执行,但如果我添加新行,则第 2 行 并使用已经有“完成”字符串的文本字段填写规范编号 2 的数据,脚本始终执行规范编号 1