How to give focus() after an alert()?

后端 未结 2 2051

I have something like:




        
2条回答
  •  被撕碎了的回忆
    2021-02-14 10:39

    Adding a small timeout and resetting the focus back to the partNumber textbox should do the trick.

    Thus your validatePart() function becomes something like:

    function validatePart(node) {
        if (!node.value) {
            alert('Please enter a part number.');
            setTimeout(function(){node.focus();}, 1);
        }
    }
    

    Here's a quick "live" example that simply fires an alert no matter what is entered into the partNumber textbox and successfully returns focus back to the partNumber textbox (even if you tab off it to the quantity textbox.

提交回复
热议问题