简体   繁体   English

Uncaught TypeError:无法读取未定义的属性“ find”

[英]Uncaught TypeError: Cannot read property 'find' of undefined

Good day, I'm using jconfirm to my website project but I'm facing a weird problem that I can't solve by my own, please see my code below. 美好的一天,我在网站项目中使用了jconfirm ,但遇到了一个我自己无法解决的怪异问题,请参见下面的代码。

$.confirm({
    title: 'Add Patient',
   theme: 'material',
    backgroundDismissAnimation: 'glow',
    type:'blue',
    typeAnimated: true,
    content: '' +
    '<form action="" class="formName" style ="margin: 10px;">' +
    '<div class="form-group">' +
    '<label>ID NUMBER</label>' +
    '<input type="text" class="id_number form-control" value="my id" required />' +
    '</div>' +
    '</form>',
    buttons: {
        formSubmit: {
            text: 'Submit',
            btnClass: 'btn-blue',
            action: function () {
            }
        },
        cancel: function () {
            //close
        },
    },
    onContentReady: function () {
        this.$content.find('.id_number').change(function(){
            var a = this.$content.find('.id_number').val();
             alert(a);
        });
    }
});

If you try to run that code it will return an error says. 如果您尝试运行该代码,则会返回错误提示。

Uncaught TypeError: Cannot read property 'find' of undefined Uncaught TypeError:无法读取未定义的属性“ find”

But the weird thing is if I change the code like this. 但是奇怪的是,如果我像这样更改代码。

onContentReady: function () {
                var a = this.$content.find('.id_number').val();
                 alert(a);
        }

The error is gone and the alert pop-up. 错误消失了,并且警报弹出。

My problem is how can I get the value of input inside the change() method? 我的问题是如何在change()方法中获取输入的值? please help or what is the correct way to make this code works? 请提供帮助,或者使该代码正常工作的正确方法是什么?

onContentReady: function () {
            this.$content.find('.id_number').change(function(){
                var a = this.$content.find('.id_number').val();
                 alert(a);
            });

To supplement the other response, which should be marked as correct, there are three ways to define this . 为了补充应被标记为正确的其他响应,可以使用三种方法来定义this响应。

  1. Function call (eg, f()) - this refers to the global object. 函数调用(例如f())-引用全局对象。

  2. Method call (eg, af()) - this refers to the object to the left of the dot (sometimes called the receiving object). 方法调用(例如,af())-这是指点左侧的对象(有时称为接收对象)。

  3. New (constructor) call - this refers to a newly allocated object. 新建(构造函数)调用-引用新分配的对象。

Your .change(...) call is an example of the second in the list above. 您的.change(...)调用是上面列表中第二个示例。 @sabithpocker's solution saves the reference to this before the value of this is changed. @ sabithpocker的解决方案节省了参考this前值this改变。

The value of this is different inside the change() function, to check that you can log the value. change()函数中, this值不同,以检查是否可以记录该值。

As a workaround, you can keep a reference to it and use the reference. 解决方法是,可以保留对此的引用并使用该引用。

You can also use bind() to bind the value of this to the event handler. 您还可以使用bind()this的值绑定到事件处理程序。 Or check different other ways using call() or apply . 或使用call()apply检查其他不同方式。

onContentReady: function() {
    var myReferenceToThis = this;
    this.$content.find('.id_number').change(function() {
      var a = myReferenceToThis.$content.find('.id_number').val();
      alert(a);
});

Or in your case, as @Phil suggested, simply do... 或者就您而言,如@Phil所建议的,只需...

onContentReady: function() {
    var myReferenceToThis = this;
    this.$content.find('.id_number').change(function() {
      var a = $(this).val();
      alert(a);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性“数量” - Uncaught TypeError: Cannot read property 'quantity' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;fromJSON&#39; - Uncaught TypeError: Cannot read property 'fromJSON' of undefined 未捕获的TypeError:无法读取未定义的属性“ timing” - Uncaught TypeError: Cannot read property 'timing' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;formatter&#39; - Uncaught TypeError: Cannot read property 'formatter' of undefined Uncaught TypeError:无法读取未定义的属性“ stopVideo” - Uncaught TypeError: Cannot read property 'stopVideo' of undefined 未捕获的类型错误:无法读取未定义的属性“setCrossOrigin” - Uncaught TypeError: Cannot read property 'setCrossOrigin' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;NaN&#39; - Uncaught TypeError: Cannot read property 'NaN' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;getAttribute&#39; - Uncaught TypeError: Cannot read property 'getAttribute' of undefined 未捕获的TypeError:无法读取未定义的属性“地理编码” - Uncaught TypeError: Cannot read property 'geocode' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM