Auto select an element and focus out of the element jquery

时间:2017-11-01 04:38:18

标签: javascript jquery jquery-validate

I need to select an element automatically and then quickly focus out of it too.

Here's my HTML:

<div class='input-group date' id='join_date'>
  <input type='text' class="form-control dateField" name="join_date" placeholder="MM/DD/YYYY" data-toggle="tooltip" data-placement="top" value="" />
  </span>
</div>

I tried putting in focus into the element but don't know how to focus out of it.

unhighlight: function(element, errorClass, validClass)
    {
      if($(element).hasClass('select2-offscreen'))
      {
        $(element).closest('.form-group').removeClass('has-error');
        $(element).next('span').css({display: "none"});
      }
      else if($(element).hasClass('dateField'))
      {
         $(element).select();
         //I need to focus out of this element quickly.. there's a reason
      }
      else
      {
        $(element).closest('.form-group').removeClass('has-error');
      }   
    },

How can I achieve auto-focusing out of the element? Please help me.

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery blur()

功能,它将从焦点元素中移除焦点。

&#13;
&#13;
$("#focus").click(function(){
  $("input").focus();
});

$("#blur").click(function(){
  $("input").blur();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='input-group date' id='join_date'>
  <input type='text' class="form-control dateField" name="join_date" placeholder="MM/DD/YYYY" data-toggle="tooltip" data-placement="top" value="" />
</div>

<button id="focus">Focus</button>
<button id="blur">Blur (Focus Out)</button>
&#13;
&#13;
&#13;