【问题标题】:Object doesn't support trim method in IE8对象不支持 IE8 中的修剪方法
【发布时间】:2014-03-11 13:15:12
【问题描述】:

我已经通过 javascript 编写了一个表单验证,它适用于除 ie8 之外的所有浏览器。我在 IE8 中看到的错误是:Object 不支持此属性或方法 并指向带有“var firstnameObject”的行。此错误在页面加载后立即显示。还有比这更多的代码,但是我不想用不必要的内容来填充这个问题。

是否有人对我为什么会收到此错误有任何线索/建议?任何帮助表示赞赏,谢谢!

var generalMethods = {
    //Validate Alpha
    validateAlphaFields : function(field){
                console.log(field)
                var currentValue = $(v[field]).val();
                console.log('current value:' + currentValue);
                    if (!currentValue || !currentValue.match(/^[A-z]+$/)){
                        $(v[field]).addClass('Invalid');
                        $('#s-'+ field).html(eval(field + 'Object.labelName'));
                    } else{
                        $(v[field]).removeClass('Invalid');
                        $('#s-'+ field).empty();
                    }
                return generalMethods.testSubmit();
    }
    //Enable submit button if parameter === 'enable', else disable submit button
    ,disableEnable : function(condition){
                if (condition === 'enable'){
                    //Enable submit button
                    $('#form_submit_button, #form-submit-button').removeAttr('disabled');
                }
            }
    //Test all fields to enable Submit Button
    ,testSubmit : function(){
                var invalidCount = 0;
                $('#errorMessage span').each(function(){
                    if ($(this).text().trim().length){
                        invalidCount++;
                    }
                });
                    //Disable submit if any invalid fields
                    if (invalidCount < 1){
                        generalMethods.disableEnable('enable');
                        $('#errorMessage').css('display','none');
                        return true;
                    } else{
                        $('#errorMessage').css('display','block');
                        return false;
                    }
                }
}

//Validate Fields Objects
//=====================================================
var firstnameObject = {
            labelName : $('#sfield_firstname label').text().trim()
            ,validate : generalMethods.validateAlphaFields
}

【问题讨论】:

  • 请提供您看到的整个错误。听起来您需要调试对象并找出哪个为空或缺少您需要的属性

标签: javascript jquery object methods internet-explorer-8


【解决方案1】:

Stringtrim 方法是在IE9 中添加的,所以在IE8 中不可用。使用$.trim() 代替以实现跨浏览器兼容性:

var firstnameObject = {
            labelName : $.trim($('#sfield_firstname label').text()),
            ,validate : generalMethods.validateAlphaFields
}

【讨论】:

    【解决方案2】:
    $(document).ready(function() {
        $('#btnsubmit').click(function() {
    
            if ($.trim($('#TextCust').val()).length == 0) {
                alert("Please Enter Customer Name");
                return false;
            }
    
        });
    });
    

    这就是你应该如何使用验证,它适用于所有浏览器。

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多