0

Here is my simple data

John Smith            Individual            010987654

I have three textboxes and the above data will automatically insert in the first textbox of my web page.

My problem is

How can I make as soon as data is inserted in the textbox (means when textbox’s onchange event is fired)

  1. First, javascript will find ‘tab’ space in this string
  2. Second, if find ‘tab’ space in the string, javascript will press ‘tab’ key and insert data in the another text box.
4

4 回答 4

1

这是一个简单的旧 DOM-0 JavaScript 解决方案,只是为了好玩。

document.getElementById('the_form').onchange = function() {
    var field = this[0];
    var parts = field.value.split('\t');
    for (var i = 0; field = this[i]; i++) {
        field.value = parts[i] || '';
    }
}

http://jsfiddle.net/vKaxP/

于 2013-05-06T04:54:38.120 回答
1

我以为你想把这些文本分成不同的文本框,所以我得到了类似的东西:

$("#a").change(function(){
    var s = $(this).val();
    if (s.match(/\t+/)) {
        var a = s.split(/\t+/);
        $('#a').val(a[0]);
        $('#b').val(a[1]);
        $('#c').val(a[2]);
    }
});

如果您a b c在第一个输入框中键入,请按tabenterb然后c会分别出现在其他文本框中。

我在jsfiddle\s中使用(space) 进行测试。您可以将其更改为选项卡。\t

于 2013-05-06T04:45:53.940 回答
0

如果我将问题正确理解为“服务器将所有数据放入一个字段中,制表符分隔,并且我想将其拆分为多个文本字段”,那么试试这个:

负载:

var fields = [$("#firstField"), $("#secondField"), $("#thirdField")];
var data = fields[0].val().split(/\t/);

for (var i = 0; i < 3; i++) {
    fields[i].val(data[i]);
}
于 2013-05-06T04:46:20.010 回答
0

这是您需要做的事情的原型。

HTML:

<div>
    <input id="a" />
</div>
<div>
    <input id="b" />
</div>

JavaScript:

$('#a').on('change', function () {
    var value = $(this).val();
    // Test if string has a tab:
    if (/\t/.test(value)) {
        // Just set the value of the other text box
        // And set focus:
        // Using jQuery that would be:
        $('#b').val(value).focus();
    }
});

工作演示:http: //jsfiddle.net/tkirda/XmArP/

于 2013-05-06T04:40:19.147 回答