【问题标题】:IE specific - blinking cursor disappears in input of type text when set to "" by jquery特定于 IE - 当 jquery 设置为“”时,闪烁的光标在输入文本时消失
【发布时间】:2011-04-20 21:19:58
【问题描述】:

所以我有一个文本类型的输入字段,它有一个默认值 (YYYY),当它获得焦点时,YYYY 会消失:

//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("div.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" />

文本消失了,光标应该在字段中闪烁。 这在 FF 中效果很好,但在 IE8 中光标不会闪烁(并且消失了)。焦点保持在该字段上,一旦用户键入内容,光标就会返回,但在此之前光标不存在。

我正在使用 jquery 1.4.4 和 IE8。我已经能够单独重新创建它(即页面上没有其他内容)

我试过 $("#year").focus() -- 没有骰子。我可以专注于其他领域并让它出现在那里,但我真的不想为了解决这个问题而转移焦点。

问题:有没有办法让IE8中的YYYY清除后光标闪烁?

编辑: 感谢您的回复 - 我已经修改以包含更多相关细节。

【问题讨论】:

  • 旁注,您的条件是多余的 - yearVal 不可能是 "YYYY""" 所以在检查之前没有必要确保它不是 ""它是"YYYY"

标签: jquery html internet-explorer-8


【解决方案1】:

试试这个:

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

这在 IE8 中对我来说效果很好:)

【讨论】:

  • 呃!很好的答案 - 不知道为什么我没有首先尝试这个。盯着屏幕看太多了。
【解决方案2】:

我刚刚在 FF4.0 和 IE8、7 和 6 中检查了以下代码。对我来说工作正常...

JavaScript:

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

HTML:

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

你的例子太过分了。例如,yearVal 来自哪里?另外,就像提到的 jball 一样,您的条件逻辑不正确

编辑:(在Win2008 Server 64bit下检查)

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 2011-02-24
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多