简体   繁体   English

在按Tab时追加输入字段

[英]On Press Tab append input field

I working in an attendance register where I have a table with a date on which the user should enter the work duration like 1 or 1.5 or 2 which means 1 as a full day, 1.5 as one and half day and so on. 我在考勤机上工作,那里有一个表,用户在该表上输入工作时间,如1或1.5或2,这意味着1为一整天,1.5为一整天和半天,依此类推。

The Problem which I am facing is I have javascript where I have given on click to append the input field. 我面临的问题是我在单击附加输入字段时给出了javascript。 but what I am looking for is whenever the user press tab it should go to the next input field. 但是我正在寻找的是每当用户按下选项卡时,它都应该转到下一个输入字段。

Please take a look at the screenshot of the layout. 请查看布局的屏幕截图。

布局的屏幕截图

Here is javascript code : 这是javascript代码:

$(function () { 
        $("td").on("click", function() {
        var $this = $(this);
        var tdID = $(this).attr("id");
        var $input = $("<input>", {
        value: $this.text(),
        type: "text",
        style: "width:40px",
        id:"myInput",
        blur: function() { 
        $this.text(this.value);
        saveEidatedData(tdID);
    },
    keyup: function(e) {
     if ((e.which === 13)&&(e.which === 9)) $input.blur();
    },
      }).appendTo( $this.empty() ).focus();
  });
});

Any help will be highly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

I think something like this? 我觉得这样吗? You may also use arrow keys, to remove the arrow key functionality just delete it from the 'arr' array 您也可以使用箭头键来删除箭头键功能,只需将其从“ arr”数组中删除即可

 var sftDown = false; $(document).on({ keydown: function(e) { //37 - 40 = arrows keys //13 = enter //9 = tab ( should auto move with tab) var arr = [9,13,37,38,39,40]; if ( e.which == 16 ) sftDown = true; if ( $.inArray(e.which, arr) !== -1 ) { var main = $(this).closest('#table'); var rows = main.find('tr'); var cells = main.find('input[type="text"]'); var downAmount = cells.length/rows.length; var move = 1; switch (e.which) { case 9: move = 0; break; case 40: case 13: move = downAmount; break; case 38: move = downAmount * -1; break; case 37: move *= -1; break; } if ( sftDown ) move *= -1; var i = cells.index(this) + move; if ( i >= cells.length ) i %= cells.length; if ( i < 0 ) i = cells.length + i; cells[i].focus(); } }, keyup: function(e) { if ( e.which == 16 ) sftDown = false; } },'#table input[type="text"]'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody id="table"> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> <tr> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> <td><input type="text" /></td> </tr> </tbody> </table> 

instead of doing it in JavaScript, you should populate the day with the text box having attribute of tabindex starting from some number and increment it to +1 for each row it should be start from where the last row tabindex ended. 而不是在JavaScript中执行此操作,您应该使用具有tabindex属性(从某个数字开始)的文本框填充当天的日期,并将每一行的tabindex从最后一行tabindex结束的地方开始增加至+1。

if you doing it in JavaScript it would take extra time to make it work also you need to place extra checks. 如果您使用JavaScript进行操作,则需要花费额外的时间才能使其正常工作,并且您还需要进行额外的检查。

like below example click on the first field and press tab it will move to the next input field. 像下面的示例一样,单击第一个字段,然后按tab键,它将移至下一个输入字段。

To do as per my suggestion, it helps you to update time easily for specific day, as well as to populate the day's from array / object data type. 按照我的建议,它可以帮助您轻松地更新特定日期的时间,以及从数组/对象数据类型填充日期。

 <tr> <td><input type="text" tabindex="1" value="1" /></td> <td><input type="text" tabindex="2" value="2" /></td> <td><input type="text" tabindex="3" value="3" /></td> </tr> <tr> <td><input type="text" tabindex="4" value="4" /></td> <td><input type="text" tabindex="5" value="5" /></td> <td><input type="text" tabindex="6" value="6" /></td> </tr> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM