English 中文(简体)
输入密钥按键更改光标位置 javaScript 时?
原标题:On enter key press change cursor position javaScript?

I have form with two text 字段和 on Load=" document. forms.obrazac.sifra. fool.point () 我将光标放在第一个字段上。 现在我要当用户按键输入鼠标时, 将光标聚焦在第二个字段上, 然后当再次按下输入时, 我要提交我的表格。 谢谢。

最佳回答

打破默认行为绝对不好。 Btw, 您知道 HTML 中的 < code> 自动聚焦 属性吗?

如果你绝对需要这个, 给你:

document.forms.obrazac.onkeypress = function( e ) {
    // If the hit key is "Enter"
    if ( e.keyCode === 13 ) {

        // Cross-browser handling for our dear friend @MaxArt :p
        var evt = e || window.event,
            target = evt.target || evt.srcElement,

        // Find the next input
            nextInput = target.nextSibling;
        while ( nextInput.tagName !==  INPUT  && nextInput.nextSibling ) {
            nextInput = nextInput.nextSibling;
        }

        // And focus it
        nextInput.focus();

        // Finally, disable submitting IF there is no input after
        if ( nextInput !== this.elements[ this.elements.length - 1 ] ) {
            return false;
        }
    }
};
问题回答

短短的不真实的睾丸样本... 只是为了给一个想法:

<head>
    <script type="text/javascript">
    function onKeyPress(args)
    {
        if(args.keyCode === 13)
            document.getElementById("tb2").focus();
    }       
    </script>
</head>
<body>
    <input type="text" onkeypress="onKeyPress(event);" id="tb1" />
    <input type="text" id="tb2" />
</body>

You could do the same on tb2 to submit the form there on "ENTER" I would also use something like jQuery to bind the events in javascript, not directly in the markup.

希望能帮上忙

@as i 开始在无答案的地方建立我的样本 =

我不会打破默认行为, 这是相当烦人 和反直觉的用户, 但是如果你真的想做到这一点, 你可以做这样的事情 与jquery:

$( #form ).keydown(function(e) {
    if( e.keyCode === 13) {  // When "Shift + Enter"
        e.preventDefault();
        // focus on next field here.
    }
});

而不是这样做,你最好只做正确的制表单 这样(用制表单):

<input type="text" name="field1" tabindex=1 /><br />

尝试此 :

document.forms.obrazac.sifra.addEventListener("keyup", function(e) {
    if (e.keyCode === 13) { // 13 === enter key
        e.preventDefault();
        this.nextSibling.focus();
    }
});

注意这些事情:

  1. I used addEventListener, but in IE8 and older you have to use attachEvent("onkeyup"... and checking the global event object. Moreover, e.preventDefault(); becomes e.returnValue = false;
  2. I supposed that this.nextSibling is the next field. Correct this to fit you DOM.


上一篇:
下一篇:


相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签