How to remove focus from input field in jQuery?

后端 未结 5 1314
故里飘歌
故里飘歌 2020-12-23 23:54

I am setting the focus on a certain input field when loading a page using the following line:

$(\'#myInputID\').focus();

Is there a way th

相关标签:
5条回答
  • 2020-12-24 00:25

    Use .blur().

    The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input>. In recent browsers, the domain of the event has been extended to include all element types. An element can lose focus via keyboard commands, such as the Tab key, or by mouse clicks elsewhere on the page.

    $("#myInputID").blur(); 
    
    0 讨论(0)
  • 2020-12-24 00:27

    check up blur():

    $('#textarea').blur()
    

    source: http://api.jquery.com/blur/

    0 讨论(0)
  • 2020-12-24 00:31

    If you have readonly attribute, blur by itself would not work. Contraption below should do the job.

    $('#myInputID').removeAttr('readonly').trigger('blur').attr('readonly','readonly');
    
    0 讨论(0)
  • 2020-12-24 00:33

    $(':text').attr("disabled", "disabled"); sets all textbox to disabled mode. You can do in another way like giving each textbox id. By doing this code weight will be more and performance issue will be there.

    So better have $(':text').attr("disabled", "disabled"); approach.

    0 讨论(0)
  • 2020-12-24 00:43

    For all textbox :

    $(':text').blur()
    
    0 讨论(0)
提交回复
热议问题