【问题标题】:Set focus on specific cell of last row of a table when text box value changes当文本框值更改时,将焦点设置在表格最后一行的特定单元格上
【发布时间】:2018-03-07 06:33:18
【问题描述】:

我想做的事!!

我在页面上有一个文本框,页面加载焦点将设置到文本框中,我将扫描一项并将其数字插入文本框。基于值,它将在 html 中添加一行表。同时我想将焦点放在“CODE”单元格文本框上。

因此,每次我扫描某些内容时,它都会在下表中插入一行,并且每次我想将焦点设置在最后一行的 CODE 列的单元格上。在代码列中,我们将扫描该值并将其显示在同一个单元格。

如果可能的话,我想在扫描代码单元值中的值后将焦点设置回文本框。所以基本上是它的来回扫描过程。

<input type="text" name=" " value=" ">
<br>
<div class="t-Report-tableWrap">
  <table class="t-Report-report" summary="Text Test">
    <thead>
      <tr>
        <th class="t-Report-colHead"  align="center"  id="DERIVED$01" >&nbsp;</th>
        <th class="t-Report-colHead"  align="center"  id="C003" >Id</th>
        <th class="t-Report-colHead"  align="center"  id="C001" >Name</th>
        <th class="t-Report-colHead"  align="center"  id="C002" >Code</th>
        <th class="t-Report-colHead"  align="center"  id="C007" >Text</th>
      </tr>
    </thead>
    <tbody>
      <tr id="init_row_invoice" style="display:none;" >
        <td class="t-Report-cell"  headers="DERIVED$01">
          <a href ="javascript:void();" class="fa fa-trash-o"></a>
        </td>
        <td class="t-Report-cell" headers="C003">(null)</td>
        <td class="t-Report-cell" headers="C001">(null)</td>
        <td class="t-Report-cell" headers="C002">
          <label for="f02_0000" id="f02_0000_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f02" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text" id="f02_0000" />
        </td>
        <td class="t-Report-cell"  headers="C007">
          <label for="f03_0000" id="f03_0000_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f03" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text" id="f03_0000" />
          <input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden" id="f01_0000" />
          <input type="hidden" name="f04" value="" class="u-TF-item u-TF-item--hidden" id="f04_0000" />
          <input type="hidden" id="fcs_0000" name="fcs" value="">
          <input type="hidden" id="fcud_0000" name="fcud" value="D" />
        </td>
      </tr>
      <tr>
        <td class="t-Report-cell"  headers="DERIVED$01">
          <a href ="javascript:void(1);" class="fa fa-trash-o"></a>
        </td>
        <td class="t-Report-cell" headers="C003">90001</td>
        <td class="t-Report-cell" headers="C001">Samsung Galaxy J3 Pro</td>
        <td class="t-Report-cell"  headers="C002">
          <label for="f02_0001" id="f02_0001_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f02" size="15" maxlength="2000" value="123456" class="u-TF-item u-TF-item--text" id="f02_0001" />
        </td>
        <td class="t-Report-cell"  headers="C007">
          <label for="f03_0001" id="f03_0001_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f03" size="15" maxlength="2000" value="1" class="u-TF-item u-TF-item--text" id="f03_0001" />
          <input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden" id="f01_0001" />
          <input type="hidden" name="f04" value="1" class="u-TF-item u-TF-item--hidden" id="f04_0001" />
          <input type="hidden" id="fcs_0001" name="fcs" value="9F677BEDD6F6624C586A9DA8BB60AA3DC1746ED2">
          <input type="hidden" id="fcud_0001" name="fcud" value="U" />
        </td>
      </tr>
    </tbody>
  </table>
</div>
<div class="t-Report-links"></div>
<table class="t-Report-pagination t-Report-pagination--bottom" role="presentation">
  <tr>
    <td colspan="9" align="right" >
      <table role="presentation" >
        <tr>
          <td class="pagination"></td>
          <td class="pagination"></td>
          <td nowrap="nowrap" class="pagination">
            <span class="t-Report-paginationText">1 - 3</span>
          </td>
          <td class="pagination"></td>
          <td class="pagination"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</div>

   

为了更清楚。

从贴纸中扫描值,它会将值插入文本框,现在它会在表格中添加一个 rwo,同时它将再次将焦点设置到最后一行的单元格列 CODE 我们将从贴纸中扫描值,扫描后它应该将焦点设置回文本框。

【问题讨论】:

  • @HemaNandagopal 我卡在设置焦点上。我可以追加行。
  • 在添加时您是否尝试过 $('#C002').focus(); ?
  • C002 是 th 和表格单元格它是 f02_0001 所以我试过这个 $('#f02_0001 ').focus();因为我想专注于细胞但没有运气
  • $("'#f02_0001 输入").focus();将焦点设置为输入怎么样?

标签: javascript jquery html css


【解决方案1】:

给你一个解决方案

$('table.t-Report-report')
  .find('tr')
  .last()
  .find('input[type="text"]')
  .first()
  .focus();

$('input').first().focus();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name=" " value=" "><br>
<div class="t-Report-tableWrap">
<table class="t-Report-report" summary="Text Test">
  <thead>
    <tr>
      <th class="t-Report-colHead"  align="center"  id="DERIVED$01" >&nbsp;</th>
      <th class="t-Report-colHead"  align="center"  id="C003" >Id</th>
      <th class="t-Report-colHead"  align="center"  id="C001" >Name</th>
      <th class="t-Report-colHead"  align="center"  id="C002" >Code</th>
      <th class="t-Report-colHead"  align="center"  id="C007" >Text</th>
    </tr>
  </thead>
  <tbody>
    <tr id="init_row_invoice" style="display:none;" >
      <td class="t-Report-cell"  headers="DERIVED$01"><a href ="javascript:void();" class="fa fa-trash-o"></a></td><td class="t-Report-cell"  headers="C003">(null)</td>
      <td class="t-Report-cell"  headers="C001">(null)</td>
      <td class="t-Report-cell"  headers="C002"><label for="f02_0000" id="f02_0000_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f02" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text " id="f02_0000" /></td>
      <td class="t-Report-cell"  headers="C007"><label for="f03_0000" id="f03_0000_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f03" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text "  id="f03_0000" /><input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden "  id="f01_0000" /><input type="hidden" name="f04" value="" class="u-TF-item u-TF-item--hidden "  id="f04_0000" /><input type="hidden" id="fcs_0000" name="fcs" value=""><input type="hidden" id="fcud_0000" name="fcud" value="D" />
      </td>
    </tr>
    <tr>
        <td class="t-Report-cell"  headers="DERIVED$01"><a href ="javascript:void(1);" class="fa fa-trash-o"></a></td>
        <td class="t-Report-cell"  headers="C003">90001</td>
        <td class="t-Report-cell"  headers="C001">Samsung Galaxy J3 Pro</td>
        <td class="t-Report-cell"  headers="C002"><label for="f02_0001" id="f02_0001_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f02" size="15" maxlength="2000" value="123456" class="u-TF-item u-TF-item--text "  id="f02_0001" /></td>
        <td class="t-Report-cell"  headers="C007">
          <label for="f03_0001" id="f03_0001_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f03" size="15" maxlength="2000" value="1" class="u-TF-item u-TF-item--text " id="f03_0001" />
          <input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden "  id="f01_0001" /><input type="hidden" name="f04" value="1" class="u-TF-item u-TF-item--hidden "  id="f04_0001" /><input type="hidden" id="fcs_0001" name="fcs" value="9F677BEDD6F6624C586A9DA8BB60AA3DC1746ED2"><input type="hidden" id="fcud_0001" name="fcud" value="U" />
        </td>
      </tr>
    </tbody>
  </table>
</div>
<div class="t-Report-links"></div>
  <table class="t-Report-pagination t-Report-pagination--bottom" role="presentation"><tr><td colspan="9" align="right" ><table role="presentation" ><tr><td class="pagination"></td><td class="pagination"></td><td nowrap="nowrap" class="pagination"><span class="t-Report-paginationText">1 - 3</span></td><td class="pagination"></td><td class="pagination"></td></tr></table></td></tr></table>
</div>

希望这会对你有所帮助。

【讨论】:

  • 这看起来不错,我选择了 [name=f02].last.focus();在您在 CODE 单元格中设置值后,它现在只剩下第二部分了,将焦点设置回文本字段
  • @PranavShah 你在说哪个文本字段,我现在有点困惑。你说的是顶部输入文本框吗?
  • 这里有解决方案$('input').first().focus();。我也更新了答案。
  • 这将集中在哪里!!
  • @PranavShah 这将聚焦在表格顶部的第一个输入文本框。
【解决方案2】:

长度 > 0

$(document).on('click', '#btn_process', function() {
  var tr = $('#init_row_invoice').clone(true);
  $('table.t-Report-report tbody').append(tr.show());
  $(tr).find('input[type=text][name=f03]').val('123');
  if ($(tr).find('input[type=text][name=f03]').val().length > 0) {
    $(tr).find('input[type=text][name=f02]').focus();
  } else {}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name=" " value=" "><input id="btn_process" type="submit" value="process" /?<br>
<div class="t-Report-tableWrap">
  <table class="t-Report-report" summary="Text Test">
    <thead>
      <tr>
        <th class="t-Report-colHead" align="center" id="DERIVED$01">&nbsp;</th>
        <th class="t-Report-colHead" align="center" id="C003">Id</th>
        <th class="t-Report-colHead" align="center" id="C001">Name</th>
        <th class="t-Report-colHead" align="center" id="C002">Code</th>
        <th class="t-Report-colHead" align="center" id="C007">Text</th>
      </tr>
    </thead>
    <tbody>
      <tr id="init_row_invoice" style="display:none;">
        <td class="t-Report-cell" headers="DERIVED$01">
          <a href="javascript:void();" class="fa fa-trash-o"></a>
        </td>
        <td class="t-Report-cell" headers="C003">(null)</td>
        <td class="t-Report-cell" headers="C001">(null)</td>
        <td class="t-Report-cell" headers="C002"><label for="f02_0000" id="f02_0000_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f02" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text " id="f02_0000" /></td>
        <td class="t-Report-cell" headers="C007">
          <label for="f03_0000" id="f03_0000_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f03" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text " id="f03_0000" />
          <input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden " id="f01_0000" />
          <input type="hidden" name="f04" value="" class="u-TF-item u-TF-item--hidden " id="f04_0000" />
          <input type="hidden" id="fcs_0000" name="fcs" value=""><input type="hidden" id="fcud_0000" name="fcud" value="D" />
        </td>
      </tr>
      <tr>
        <td class="t-Report-cell" headers="DERIVED$01">
          <a href="javascript:void(1);" class="fa fa-trash-o"></a>
        </td>
        <td class="t-Report-cell" headers="C003">90001</td>
        <td class="t-Report-cell" headers="C001">Samsung Galaxy J3 Pro</td>
        <td class="t-Report-cell" headers="C002"><label for="f02_0001" id="f02_0001_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f02" size="15" maxlength="2000" value="123456" class="u-TF-item u-TF-item--text " id="f02_0001" /></td>
        <td class="t-Report-cell" headers="C007"><label for="f03_0001" id="f03_0001_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f03" size="15" maxlength="2000" value="1" class="u-TF-item u-TF-item--text " id="f03_0001" /><input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden "
            id="f01_0001" /><input type="hidden" name="f04" value="1" class="u-TF-item u-TF-item--hidden " id="f04_0001" /><input type="hidden" id="fcs_0001" name="fcs" value="9F677BEDD6F6624C586A9DA8BB60AA3DC1746ED2"><input type="hidden" id="fcud_0001"
            name="fcud" value="U" />
        </td>
      </tr>

    </tbody>
  </table>
</div>
<div class="t-Report-links"></div>
<table class="t-Report-pagination t-Report-pagination--bottom" role="presentation">
  <tr>
    <td colspan="9" align="right">
      <table role="presentation">
        <tr>
          <td class="pagination"></td>
          <td class="pagination"></td>
          <td nowrap="nowrap" class="pagination"><span class="t-Report-paginationText">1 - 3</span></td>
          <td class="pagination"></td>
          <td class="pagination"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>

长度 == 0

$(document).on('click', '#btn_process', function() {
  var tr = $('#init_row_invoice').clone(true);
  $('table.t-Report-report tbody').append(tr.show());
  $(tr).find('input[type=text][name=f03]').val('');
  if ($(tr).find('input[type=text][name=f03]').val().length > 0) {
    $(tr).find('input[type=text][name=f02]').focus();
  } else {}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name=" " value=" "><input id="btn_process" type="submit" value="process" /?<br>
<div class="t-Report-tableWrap">
  <table class="t-Report-report" summary="Text Test">
    <thead>
      <tr>
        <th class="t-Report-colHead" align="center" id="DERIVED$01">&nbsp;</th>
        <th class="t-Report-colHead" align="center" id="C003">Id</th>
        <th class="t-Report-colHead" align="center" id="C001">Name</th>
        <th class="t-Report-colHead" align="center" id="C002">Code</th>
        <th class="t-Report-colHead" align="center" id="C007">Text</th>
      </tr>
    </thead>
    <tbody>
      <tr id="init_row_invoice" style="display:none;">
        <td class="t-Report-cell" headers="DERIVED$01">
          <a href="javascript:void();" class="fa fa-trash-o"></a>
        </td>
        <td class="t-Report-cell" headers="C003">(null)</td>
        <td class="t-Report-cell" headers="C001">(null)</td>
        <td class="t-Report-cell" headers="C002"><label for="f02_0000" id="f02_0000_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f02" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text " id="f02_0000" /></td>
        <td class="t-Report-cell" headers="C007">
          <label for="f03_0000" id="f03_0000_LABEL" class="u-VisuallyHidden"></label>
          <input type="text" name="f03" size="15" maxlength="2000" value="" class="u-TF-item u-TF-item--text " id="f03_0000" />
          <input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden " id="f01_0000" />
          <input type="hidden" name="f04" value="" class="u-TF-item u-TF-item--hidden " id="f04_0000" />
          <input type="hidden" id="fcs_0000" name="fcs" value=""><input type="hidden" id="fcud_0000" name="fcud" value="D" />
        </td>
      </tr>
      <tr>
        <td class="t-Report-cell" headers="DERIVED$01">
          <a href="javascript:void(1);" class="fa fa-trash-o"></a>
        </td>
        <td class="t-Report-cell" headers="C003">90001</td>
        <td class="t-Report-cell" headers="C001">Samsung Galaxy J3 Pro</td>
        <td class="t-Report-cell" headers="C002"><label for="f02_0001" id="f02_0001_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f02" size="15" maxlength="2000" value="123456" class="u-TF-item u-TF-item--text " id="f02_0001" /></td>
        <td class="t-Report-cell" headers="C007"><label for="f03_0001" id="f03_0001_LABEL" class="u-VisuallyHidden"></label><input type="text" name="f03" size="15" maxlength="2000" value="1" class="u-TF-item u-TF-item--text " id="f03_0001" /><input type="hidden" name="f01" value="" class="u-TF-item u-TF-item--hidden "
            id="f01_0001" /><input type="hidden" name="f04" value="1" class="u-TF-item u-TF-item--hidden " id="f04_0001" /><input type="hidden" id="fcs_0001" name="fcs" value="9F677BEDD6F6624C586A9DA8BB60AA3DC1746ED2"><input type="hidden" id="fcud_0001"
            name="fcud" value="U" />
        </td>
      </tr>

    </tbody>
  </table>
</div>
<div class="t-Report-links"></div>
<table class="t-Report-pagination t-Report-pagination--bottom" role="presentation">
  <tr>
    <td colspan="9" align="right">
      <table role="presentation">
        <tr>
          <td class="pagination"></td>
          <td class="pagination"></td>
          <td nowrap="nowrap" class="pagination"><span class="t-Report-paginationText">1 - 3</span></td>
          <td class="pagination"></td>
          <td class="pagination"></td>
        </tr>
      </table>
    </td>
  </tr>
</table>

检查长度如:

$(tr).find('input[type=text][name=f03]').val().length > 0

设置焦点

$(tr).find('input[type=text][name=f02]').focus();

【讨论】:

  • 有什么方法可以检查 name=f02 最后一个单元格值长度 > 0 如果是,那么再次将焦点设置回文本字段我尝试过这个但没有运气 jQuery('#mytableid tbody' ).find('td:eq(3)').last().length();
  • 我不知道该怎么做......即使我试图获得焦点单元格值的长度,因为它总是最后一个然后如果 len > 0 然后将焦点重新设置在文本框上
  • @PranavShah:请检查我更新的代码。您可以编写代码以将所需的控制集中在if length == 0else {}
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
  • 2012-04-09
相关资源
最近更新 更多