美好的一天,我正在使用 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);
});
}
});
如果您尝试运行该代码,它将返回一个错误。
Uncaught TypeError: Cannot read property 'find' of undefined
但奇怪的是,如果我像这样更改代码。
onContentReady: function () {
var a = this.$content.find('.id_number').val();
alert(a);
}
错误消失并弹出警报。
我的问题是如何获取change()方法内的输入值?请帮助或者使该代码正常工作的正确方法是什么?
onContentReady: function () {
this.$content.find('.id_number').change(function(){
var a = this.$content.find('.id_number').val();
alert(a);
});
最佳答案
为了补充应标记为正确的其他响应,可以使用三种方法来定义this
。
函数调用(例如 f())- this 引用全局对象。
方法调用(例如,a.f()) - 这是指点左侧的对象(有时称为接收对象)。
新(构造函数)调用 - 这指的是新分配的对象。
您的 .change(...)
调用是上面列表中第二个调用的示例。 @sabithpocker 的解决方案在 this
的值更改之前保存对 this
的引用。
关于javascript - 未捕获的类型错误 : Cannot read property 'find' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48514079/