javascript - Change tabindex dynamically in jquery - Stack Overflow

Imagine this simple layout (). We have a table, for each row i have a label a textbox and a checkbo

Imagine this simple layout ( / ). We have a table, for each row i have a label a textbox and a checkbox. The first textbox has tabindex = 1 , the first checkbox has tabindex = 2, the next textbox on the second row has tabindex = 3 and so on.

What i would like to do is change the tabindex on the checkbox on the same row for the textbox that im in if the content of the textbox has changed.

For example at the moment im checking the checkbox on the row that the thebox content has been changed, i do that with the following code:

 $("input:text").change(function () {
            $(this).parents('tr:first').find('input:checkbox').prop('checked', 'checked');

So the scenario is the following: If the content of the textbox hasnt changed, when i tab i want the focus to change to the checkbox on the same row. If the content of the textbox HAS changed, i would like the focus to be on the next textbox on the row below INSTEAD of the checkbox on the same row.

I hope i explained it good enough for you to understand what im trying to do. I've tried using .removeprop() and .removeattr() but that doesnt work and when next tab hits it just goes to the bottom of the page.

Imagine this simple layout ( http://jsfiddle/4F9cL/ ). We have a table, for each row i have a label a textbox and a checkbox. The first textbox has tabindex = 1 , the first checkbox has tabindex = 2, the next textbox on the second row has tabindex = 3 and so on.

What i would like to do is change the tabindex on the checkbox on the same row for the textbox that im in if the content of the textbox has changed.

For example at the moment im checking the checkbox on the row that the thebox content has been changed, i do that with the following code:

 $("input:text").change(function () {
            $(this).parents('tr:first').find('input:checkbox').prop('checked', 'checked');

So the scenario is the following: If the content of the textbox hasnt changed, when i tab i want the focus to change to the checkbox on the same row. If the content of the textbox HAS changed, i would like the focus to be on the next textbox on the row below INSTEAD of the checkbox on the same row.

I hope i explained it good enough for you to understand what im trying to do. I've tried using .removeprop() and .removeattr() but that doesnt work and when next tab hits it just goes to the bottom of the page.

Share Improve this question asked May 14, 2014 at 12:54 user3636683user3636683 231 gold badge1 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
$("input:text").change(...);

The change event of a textbox will only be executed once the textbox loses focus. This means that when you press Tab, that function has not yet run, and you'll end up with your old tabindex target.

$("input:text").keyup(...);
$("input:text").keydown(...);

These events will fire when you either press down on (keydown), or release (keyup) a button while the textbox is focused.

Something like this should do the trick:

$("input:text").keyup(function() {
    var textBox = $(this);
    var textBoxIsEmpty = textBox.val().length == 0; //I assume you use this to determine which element should be the next to get focused when pressing tab.

    var currentTabIndex = parseInt( textBox.attr("tabindex") );

    var nextTarget = GetNextTarget(textBoxIsEmpty); //not sure how you define this. This is the item you want to be your next target when you press tab.
    var nextTabIndex = currentTabIndex + 1;

    nextTarget.attr("tabindex", nextTabIndex);
});

Although I am not 100% sure if changing the tabindex dynamically works in every browser.

I have tried like that and it's working fine for me. before applying this you should clear on this you have declare you tabindex in the input properly.

jQuery("input:text").keyup(function() {    
  currentTabIndex = parseInt(jQuery(this).attr("tabindex") );
  nextTabIndex = currentTabIndex + 1;
  jQuery('input[tabindex='+nextTabIndex+']').select();
}); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744385422a4571639.html

相关推荐

  • javascript - Change tabindex dynamically in jquery - Stack Overflow

    Imagine this simple layout (). We have a table, for each row i have a label a textbox and a checkbo

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信