开发者

Focus on TextBox is triggering PageLoad using C# .net

开发者 https://www.devze.com 2023-03-10 12:21 出处:网络
I have a function that set focus to the next field via JS on the form when press enter: function pressEnter(obj, e) {

I have a function that set focus to the next field via JS on the form when press enter:

function pressEnter(obj, e) {
    var key = e.keyCode ? e.keyCode : e.which;
    if (key === 13开发者_JAVA技巧) {
        var total = document.forms[0].elements.length;
        for (var i = 0; i < total; i++) {
            if (obj.id == document.forms[0].elements[i].id) {
                index = i + 1;
                while ((document.forms[0].elements[index].type == "hidden" || document.forms[0].elements[index].nodeName == "FIELDSET" || document.forms[0].elements[index].disabled == true)) {
                    index++;
                    if (document.forms[0].elements[index] == null)
                        return;
                }
                $('#' + document.forms[0].elements[index].id).focus();
                break;
            }
        }
    }
}

In BODY tag, i have the following script: onkeypress = "var key = event.keyCode? event.keyCode: e.which; return (key! = 13);", to avoid the post when the [Enter] is pressed

Operate normally, if I set [TextBox1 autoPostBack=false] [TextBox2 autoPostBack=false]. Type in TextBox1, [enter], TextBox2 receives focus and triggers the onkeypress event of the body.

Good.

Here comes the problem

If I set [TextBox1 autoPostBack=false] [TextBox2 autoPostBack=true] when textBox2 receives the focus, it refreshes the page, executes the post, and do not trigger the onkeypress event of the body. It was not typed anything in TextBox2. I would like the TextBox with AutoPostBack = true does not make the post, or refresh the screen, until a value is entered and run the onBlur.

Detail, if press the TAB, this problem does not happen, just when set the focus programmatically via JS.

If someone can explain to me why, I'll be very grateful.

Tarsis - Varois


you need to have <form onsubmit="return false;"> for your form as mentioned here: How to prevent ENTER keypress to submit a web form?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号