开发者

IE specific - blinking cursor disappears in input of type text when set to "" by jquery

开发者 https://www.devze.com 2023-02-28 05:37 出处:网络
So I have a input field of type text that has a default value (YYYY) and when it gains focus the YYYY disappears:

So I have a input field of type text that has a default value (YYYY) and when it gains focus the YYYY disappears:

//inside document.ready
....

 $("#year").focus(function(){
    if(yearVal === "YYYY") {
        $("#year").val("");
    }
});

jQuery.validator.addMethod("numericCheck", function(value, element) {
                return this.optional(element) || /^[0-9]{4}$/.test(value);
        }, "Please enter a valid year");

$("#theForm").validate({
    //set the rules for the field names
    rules: {
       year: {
                  required: true,
                              minlength: 4,
                  numericCheck: true
            }
      },
      messages: {
        year: {
                required: "Please enter the year",
                minlength : "Please enter a valid 4 digit year",
                numericCheck : "Please enter a valid year"
                }           
      },
      errorPlacement: function(error, element) {
            if(element.siblings("div.errorMessage").length)
            {
                element.siblings("d开发者_运维百科iv.errorMessage").remove();
            }

            element.parent().append('<div class="errorMessage"></div>');
            element.siblings("div.errorMessage").html(error.html());

            element.parents("tr.inputRow").removeClass("goodInputRow");
            element.parents("tr.inputRow").addClass("errorInputRow");
          },
      onkeyup: false,
      onfocusout: false,
      onclick: false,
      submitHandler: function(form) {
            $(form).find(":submit").attr("disabled", true);
            form.submit(); }
    });


....
//inside theForm

<input type="text" id="year" name="year" style="width: 40px;" maxlength="4" value="YYYY" />

The text is gone and the cursor should be blinking in the field. This works great in FF, but in IE8 the cursor does not blink (and is gone). The focus remains on the field, and once the user types something the cursor comes back, but until that happens the cursor is not there.

I'm using jquery 1.4.4 and IE8. I've been able to recreate it in isolation (i.e. nothing else running on the page)

I have tried $("#year").focus() -- no dice. I can focus on some other field and have it appear there, but I really don't want to have to bounce the focus around to fix this.

Question: Is there a way to get the cursor blinking after clearing the YYYY in IE8?

EDIT: Thanks for the responses - I've modified to include more of the pertinent details.


Try this:

$('#year').focus(function () {
    if ($(this).val() === "YYYY") {
        $(this).val("");
        $(this).select();
    }
});

This Worked fine for me in IE8 :)


I just checked the following code in FF4.0 and IE8, 7 and 6. Works fine for me...

JavaScript:

  $('#year').focus(function () {
      if ($(this).val() === "YYYY") {
          $(this).val("");
      }
  });

HTML:

<input type="text" id="year" value="YYYY" />

Your example is too stripped out. For instance, where is yearVal coming from? Also, like jball mentioned, your condition logic is incorrect

EDIT: (Checked under Win2008 Server 64bit)

0

精彩评论

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

关注公众号