2014-02-12 28 views
1

後,得到了類似的問題,這questionIE8刪除佔位符文本驗證失敗

在所有瀏覽器的佔位符文本是罰款後驗證失敗,我的形式,但在IE8中消失。最初IE8不顯示佔位符,所以我用這個修復,填補了佔位符

$('[placeholder]') 
    .focus(function() { 
     var input = $(this); 
     if (input.val() == input.attr('placeholder')) { 
      input.val(''); 
      input.removeClass('placeholder'); 
     } 
    }) 
    .blur(function() { 
     var input = $(this); 
     if (input.val() == '' || input.val() == input.attr('placeholder')) { 
      input.addClass('placeholder'); 
      input.val(input.attr('placeholder')); 
     } 
    }) 
    .blur(); 

$('[placeholder]').parents('form').submit(function() {  
    $(this).find('[placeholder]').each(function() { 
     var input = $(this); 
     if (input.val() == input.attr('placeholder')) { 
      input.val(''); 
     } 
    }) 
}); 

我應該在第一時間使用不同的佔位符修復/填充工具?或者有沒有辦法解決這個問題?

+0

更好的將是在瀏覽器不使用的佔位符,其別不支持它。無論如何,使用佔位符並不意味着你不必使用某種描述性標籤向用戶輸出相關信息,這不是佔位符的目標。考慮可訪問性 –

+0

設計不包括標籤,必須使用佔位符和去上午是現在 – Pixelomo

+0

因此,我的老闆會說它設計得很糟糕......當然你可能不同意:) –

回答

1
var placeholders = {}; 
$('form').validate({ 
    submitHandler: function(form) { 

    $(form).find(':input[placeholder]').each(function() { 
     var placeholder = $(this).attr('placeholder'); 
     placeholders[placeholder] = this; 
     $(this).removeAttr('placeholder'); 
    });  

    form.submit(); 


}, 



enter code here 

    $.each(placeholders, function(placeholder, element) { 
     $(element).attr('placeholder', placeholder); 
    }); 

} 

}); 
0

我解決了這個重新定義驗證器的默認所需的功能,在調用驗證我加了這一點:

jQuery.validator.addMethod("required", function(value, element, param) { 
     // check if dependency is met 
     if (!this.depend(param, element)) { 
      return "dependency-mismatch"; 
     } 
     if (element.nodeName.toLowerCase() === "select") { 
      // could be an array for select-multiple or a string, both are fine this way 
      var val = $(element).val(); 
      return val && val.length > 0; 
     } 
     if (this.checkable(element)) { 
      return this.getLength(value, element) > 0; 
     } 
     default: 
      var placeholderval = $(element).attr('placeholder'); 
      //if some placeholder values are actually default values, just use "default" attribute to mark them 
      var defaultvar = ($(element).attr('default') === undefined); 
      return ($.trim(value).length > 0 && ($.trim(value)!=$.trim(placeholderval) || !defaultvar)); 
    }, jQuery.validator.messages.required);