codeigniter-3 - 无法修复无法读取未定义的属性(读取 'length' )

标签 codeigniter-3 alertifyjs

你好,我是 javascript 和 jquery 的初学者,我收到此错误:

Cannot read properties of undefined (reading 'length')

我不知道如何解决这个问题。如何修复此错误?

这是我的 js 文件:

var fname, lname, organization, designation, email, m_number;
$('#proceedBtn').on('click', function(event) {
    fname = $('#fname').val();
    lname = $('#lname').val();
    email = $('#email').val();
    phone = $('#m_number').val();
    organization = $('#organization').val();
    designation = $('#designation').val();
    //set it to true
    var isProceed = true;

    if (fname.trim() === "" || fname.length < 2) {
        alertify.error('Please input your first name');
        isProceed = false;
    }

    if (lname.trim() === "" || lname.length < 2) {
        alertify.error('Please input your last name');
        isProceed = false;
    }
    const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    var emailValid = re.test(String(email).toLowerCase());

    if (email.trim() === "" || email.length < 3 || !emailValid) {
        alertify.error('Please check your email');
        isProceed = false;
    }

    if (m_number === "" || m_number.length < 3) {
        alertify.error('Please input your phone');
        isProceed = false;
    }
    if (organization.trim() === "" || organization.length < 2) {
        alertify.error('Please input your organization');
        isProceed = false;
    }

    if (designation.trim() === "" || designation.length < 2) {
        alertify.error('Please input your designation');
        isProceed = false;
    }


    if (isProceed) {
        procceedBTN();
        // $('html, body').animate({scrollTop: '0px'}, 0);
    }
})

最佳答案

您在顶部启动了一些变量,但没有为它们定义/分配任何值

var fname, lname, organization, designation, email, m_number;

请注意,这些变量的值未定义

在函数的开头,您为 fname、lname 和 email 分配了一些值,但 m_number 仍然未定义。

在您的整个代码中,您从未为 m_number 分配任何值

但在您的代码中:

 if (m_number === "" || m_number.length < 3) {
    alertify.error('Please input your phone');
    isProceed = false;
}

您要求检查 undefined variable 的长度

m_number.length //this is the error

您可以简单地删除这部分代码,或者可以为 m_number 定义一个值

喜欢

m_number = ''

关于codeigniter-3 - 无法修复无法读取未定义的属性(读取 'length' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70357848/

相关文章:

php - 无法从 codeigniter 3 连接到 Amazon RDS 中的数据库

mysql - foreach 循环中 codeigniter 中出现 500 内部服务器错误和 undefined variable 错误?

javascript - 当点击关闭按钮时,它应该停止使用 alertify 进行重定向吗?

javascript - Alertify 警报弹出后立即消失

jquery - 在 typescript 中调用函数在alertify.js 中不起作用

php - 如何使用 codeigniter 通过 CONCAT() 函数更新数据库列值?

php - MySQL VALUES() 函数不适用于 Codeigniter db->query()

jquery - Alertify JS-更改提示对话框上的按钮文本

php - 如何上传 .webp 图像

dialog - 在alertify确认对话框中添加自定义按钮