jquery代理传递参数 [英] jquery proxy passing parameters
本文介绍了jquery代理传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
绑定事件处理程序时,如何通过代理传递字符串?我想将附加到目标处理程序的数据属性传递给对象的方法。这可能吗?提前致谢。
How can you pass a string via proxy when binding event handlers? I want to pass a data attribute that is attached to the target handler to a method of an object. Is this possible? Thanks in advance.
function ReservationSchedulePicker(reservationType){
//Reservation Type Accepted Use: 'routine' || 'vacation'
this.reservationType = reservationType;
//DIV for Schedule Picker
this.schedulePickerDiv = $("#schedulePicker");
//Add Event Handler to Anchor
$(this.schedulePickerDiv).on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
}
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event, day) {
//Trying to pass the data-day value to this function using proxy
}
//I need the data-day value inside of openAddWalkDialog Is this possible?
<a href="#" id="addWalk" data-day="monday">
推荐答案
这就是你要找的东西:
<script type="text/javascript">
function ReservationSchedulePicker(reservationType){
this.reservationType = reservationType;
this.schedulePickerDiv = $("#schedulePicker");
this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event) {
event.preventDefault();
alert(this.reservationType);
}
$(document).on('ready',function(){
var x = new ReservationSchedulePicker('hello world');
});
</script>
<div id="schedulePicker">
<a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
更新1:基于评论中提供的其他详细信息
<script type="text/javascript">
function ReservationSchedulePicker(reservationType){
this.reservationType = reservationType;
this.schedulePickerDiv = $("#schedulePicker");
this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this, $("#addWalk").attr('data-day') ));
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function(attr, event) {
event.preventDefault();
alert(this.reservationType + '=>'+ attr);
}
$(document).on('ready',function(){
var x = new ReservationSchedulePicker('hello world');
});
</script>
<div id="schedulePicker">
<a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
更新2
<script type="text/javascript">
function ReservationSchedulePicker(reservationType){
this.reservationType = reservationType;
this.schedulePickerDiv = $("#schedulePicker");
this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this ));
}
ReservationSchedulePicker.prototype.openAddWalkDialog = function( event) {
event.preventDefault();
alert(this.reservationType + '=>'+ ($(event.target).data('day'))) ;
}
$(document).on('ready',function(){
var x = new ReservationSchedulePicker('hello world');
});
</script>
<div id="schedulePicker">
<a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
这篇关于jquery代理传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文