How to set cursor to input box in Javascript?

后端 未结 8 648
长情又很酷
长情又很酷 2020-12-13 03:42
document.getElementById(frmObj.id).value=\"\";
document.getElementById(frmObj.id).autofocus;
document.getElementById(\"errorMsg\").innerHTML = \"Only numeric value i         


        
相关标签:
8条回答
  • 2020-12-13 03:44

    Sometimes you do get focus but no cursor in a text field. In this case you would do this:

    document.getElementById(frmObj.id).select();
    
    0 讨论(0)
  • 2020-12-13 03:45

    One of the things that can bite you is if you are using .onmousedown as your user interaction; when you do that, and then an attempt is immediately made to select a field, it won't happen, because the mouse is being held down on something else. So change to .onmouseup and viola, now focus() works, because the mouse is in an un-clicked state when the attempt to change focus is made.

    0 讨论(0)
  • 2020-12-13 03:47

    Inside the input tag you can add autoFocus={true} for anyone using jsx/react.

    <input
     type="email"
     name="email"
     onChange={e => setEmail(e.target.value)}
     value={email}
     placeholder={"Email..."}
     autoFocus={true}
    />
    
    0 讨论(0)
  • 2020-12-13 03:48

    I realize that this is quite and old question, but I have a 'stupid' solution to a similar problem which maybe could help someone.

    I experienced the same problem with a text box which shown as selected (by the Focus method in JQuery), but did not take the cursor in.

    The fact is that I had the Debugger window open to see what is happening and THAT window was stealing the focus. The solution is banally simple: just close the Debugger and everything is fine...1 hour spent in testing!

    0 讨论(0)
  • 2020-12-13 03:50

    This way sets the focus and cursor to the end of your input:

    div.getElementsByTagName("input")[0].focus();
    div.getElementsByTagName("input")[0].setSelectionRange(div.getElementsByTagName("input")[0].value.length,div.getElementsByTagName("input")[0].value.length,"forward");
    
    0 讨论(0)
  • 2020-12-13 03:53

    You have not provided enough code to help You likely submit the form and reload the page OR you have an object on the page like an embedded PDF that steals the focus.

    Here is the canonical plain javascript method of validating a form It can be improved with onubtrusive JS which will remove the inline script, but this is the starting point DEMO

    function validate(formObj) {
      document.getElementById("errorMsg").innerHTML = "";    
      var quantity = formObj.quantity;
      if (isNaN(quantity)) {
        quantity.value="";
        quantity.focus();
        document.getElementById("errorMsg").innerHTML = "Only numeric value is allowed";
        return false;
      }
      return true; // allow submit
    }   
    

    Here is the HTML

    <form onsubmit="return validate(this)">
        <input type="text" name="quantity" value="" />
        <input type="submit" />
    </form>    
    <span id="errorMsg"></span>
    
    0 讨论(0)
提交回复
热议问题