在ko.observable字段上处理异常“JavaScript运行时错误:无法获取未定义或空引用的属性'长度”?

时间:2017-03-23 13:49:06

标签: javascript html dom knockout.js

我在注册表单上使用Knockoutjs。用户单击按钮显示密码字段

时出现运行时错误
  

JavaScript运行时错误:无法获取未定义的属性“长度”   或空引用

如何在KO.js中处理运行时异常?

   self.showHidePasswordField = function () {

        if (self.password1().length > 0 || typeof self.password1().length !== 'undefined') {
            console.log('show');
        } else {
            console.log('hide');
        }

    };

1 个答案:

答案 0 :(得分:1)

假设您确定定义了password1可观察对象,可以尝试以下方法:

if (self.password1() != null && typeof self.password1().length !== 'undefined')