var $forms = $('form.vote');
$forms.submit(function(){
return false;
});
$('.votebtn').on('click',function(){
$this.closest('form').trigger('vote_now', [$(this).val()]);
});
$forms.on('vote_now',function(value){
alert(value);
});vote_now应该被触发并弹出值,但有些地方不对劲。那是什么?
http://jsfiddle.net/6ZHy4/
发布于 2011-12-19 13:21:42
您忘记了var $this = $(this);,额外的参数将在事件对象之后传递。
var $forms = $('form.vote');
$forms.submit(function(){
return false;
});
$('.votebtn').on('click',function(){
var $this = $(this); // declare variable
$this.closest('form').trigger('vote_now', [$(this).val()]);
});
// second argument, not first.
$forms.on('vote_now',function( e, value ){
alert(value);
});发布于 2011-12-19 13:21:19
$this声明不正确。在第6行使用$(this)
http://jsfiddle.net/PxkpC/
https://stackoverflow.com/questions/8557377
复制相似问题