绑定事件处理程序时,如何通过代理传递字符串?我想将附加到目标处理程序的数据属性传递给对象的方法。这可能吗?提前致谢。
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">
答案 0 :(得分: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);
}
$(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>
答案 1 :(得分:1)
这应该可以解决问题:
var that = this;
$(this.schedulePickerDiv).on("click", "#addWalk", function(e){
that.openAddWalkDialog.call(this, e);
});
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event) {
console.log(event, $(this).data('day'));
}
答案 2 :(得分:1)
类似于代理的参考参数:
$(this.schedulePickerDiv).on('click', '#addWalk', $.proxy(this.openAddWalkDialog, this, { foo: bar }));
或者在event.data对象(event.data.foo)上:
$(this.schedulePickerDiv).on('click', '#addWalk', { foo: bar }, $.proxy(this.openAddWalkDialog, this));