如何将`$(this)`作为参数传递给";callready“;Javascript中的函数
如何将`$(this)`作为参数传递给";callready“;Javascript中的函数,javascript,jquery,materialize,Javascript,Jquery,Materialize,leanmodel功能触发带有一些参数的模态。其中一个参数是一个函数(ready),该函数将在模式打开后执行。关键是,我需要在该函数(ready)中使用触发模式的元素(tag),做一些事情,所以我需要将$(this)作为参数传递给该函数。leanmodel()函数由MaterializeCS提供,这是我正在使用的框架 我一直在尝试这个,但是thisTag总是未定义。我还尝试直接将$(this)传递给函数,但它也根本不起作用,它仍然是未定义的。那么,我怎样才能做到这一点呢 $('.modal-tr
leanmodel
功能触发带有一些参数的模态。其中一个参数是一个函数(ready
),该函数将在模式
打开后执行。关键是,我需要在该函数(ready
)中使用触发模式的元素(tag
),做一些事情,所以我需要将$(this)
作为参数传递给该函数。leanmodel()
函数由MaterializeCS提供,这是我正在使用的框架
我一直在尝试这个,但是thisTag
总是未定义。我还尝试直接将$(this)
传递给函数,但它也根本不起作用,它仍然是未定义的。那么,我怎样才能做到这一点呢
$('.modal-trigger-editMedic').leanModal({
thisTag: $(this),
ready: function(thisTag){
var refereeNum = thisTag.siblings("[name*='refereeNumToEdit']" )[0].value;
$('#surname').val($("input[id*='medicNameToModal"+refereeNum+"'").val());
}
});
当您在leanModal中时,它会变成这样。尝试将var设置为$(this)并传递该值
var that = $(this);
$('.modal-trigger-editMedic').leanModal({
thisTag: that,
ready: function(thisTag){
var refereeNum = thisTag.siblings("[name*='refereeNumToEdit']" )[0].value;
$('#surname').val($("input[id*='medicNameToModal"+refereeNum+"'").val());
}
});
当您在leanModal中时,它会变成这样。尝试将var设置为$(this)并传递该值
var that = $(this);
$('.modal-trigger-editMedic').leanModal({
thisTag: that,
ready: function(thisTag){
var refereeNum = thisTag.siblings("[name*='refereeNumToEdit']" )[0].value;
$('#surname').val($("input[id*='medicNameToModal"+refereeNum+"'").val());
}
});
接下来,.leanmodel
支持就绪功能(模式可见时触发),但不绑定或发送触发模式的元素,解决此问题的最简单方法是在外部存储引用。为此,您需要自己迭代触发器,而不是依赖这个jQuery插件提供的功能
像这样:
var $surname = $('#surname'); // you should store the selector as a reference
// outside the loop for better performance
$('.modal-trigger-editMedic').each(function() {
var $this = $(this); // this is the current item in the set of elements,
// therefore our trigger element
// EDIT: using var makes this a local variable
$this.leanModal({
ready: function() {
var refereeNum = $this.siblings("[name*='refereeNumToEdit']" )[0].value;
$surname.val($("input[id*='medicNameToModal"+refereeNum+"'").val());
}
});
});
接下来,.leanmodel
支持就绪功能(模式可见时触发),但不绑定或发送触发模式的元素,解决此问题的最简单方法是在外部存储引用。为此,您需要自己迭代触发器,而不是依赖这个jQuery插件提供的功能
像这样:
var $surname = $('#surname'); // you should store the selector as a reference
// outside the loop for better performance
$('.modal-trigger-editMedic').each(function() {
var $this = $(this); // this is the current item in the set of elements,
// therefore our trigger element
// EDIT: using var makes this a local variable
$this.leanModal({
ready: function() {
var refereeNum = $this.siblings("[name*='refereeNumToEdit']" )[0].value;
$surname.val($("input[id*='medicNameToModal"+refereeNum+"'").val());
}
});
});
您正在将此标记定义为参数。。更改此项:
ready:function(){
是否使用leanmodel@pichfl我不知道,这是Materializecss提供的函数您将此标记定义为参数..更改此项:ready:function(){
你在使用leanmodel@pichfl吗?我真的不知道,它是Materializecss提供的一个函数是的,它包含对执行它的作用域的查询对象的引用是的,它包含对执行它的作用域的查询对象的引用。但是使用thisTag的实例:只需在ready回调中使用它,它就会工作。Ofc我们假设此时this引用tag元素。它工作,但是varreferenum
返回值to最后一次引用<代码>$(此)哪一个是最后一个.modal trigger editMedic
。列表中有一些.modal trigger editMedic
,该脚本总是返回最后一个的引用,我不知道我是否在解释自己…哦,对不起,我错过了一个相当重要的var
语句。请参阅上面的更新答案。我知道“谢谢”消息是“禁止”的,所以我只想告诉你:你是老板。这是可行的,但是varreferenum
将值返回到$的最后一个引用(此)
哪一个是最后一个.modal trigger editMedic
。列表中有一些.modal trigger editMedic
,该脚本总是返回最后一个的引用,我不知道我是否在解释自己…哦,对不起,我错过了一个相当重要的var
语句。请参阅上面的更新答案。我知道“谢谢”留言是“禁止的”,所以我只能告诉你:你是老板。