2011-02-27 70 views
3

我目前使用jQuery插件插件来创建基于标题框的项目slu g。这很好。我有麻烦做的只是当用户点击编辑链接时更新slug。现在当编辑链接被调用时,slug函数被启动并且工作正常,但是当点击“完成”链接时,我需要找到一种方法来关闭slug函数。我希望这是有道理的。如何结束点击jQuery的功能

$('#edit_slug').click(function() { 
    //allow user to edit the project slug 
    $("#edit_project_name").stringToSlug({ //this is the slug plugin 
     getPut: '.project_slug', 
     hide: false 
    }); 
    $('input.project_slug').show(); //show the input 
    $('input.project_slug').next("span").remove().end(); //remove the span with the slug 
    $('#edit_slug').hide(); //hide edit link 
    $('input.project_slug').after(" <a href='#' id='done_edit_slug'>Done</a>"); //show done link 
}); 

//if the user is done editing the slug show the span with the slug in it 
$('#done_edit_slug').live('click', function() { 
     $("#edit_project_name").stringToSlug().end(); //my attempt to make the function end 
     $('input.project_slug').after("<strong><span>"+$('input.project_slug').val()+"</span></strong>"); //show the slug as a span 
     $('input.project_slug').hide(); //hide the input box 
     $('#done_edit_slug').remove().end(); //remove done link 
     $('#edit_slug').show(); //show edit link 


}); 

我不确定这是实现这一目标的最佳方式,我愿意接受创意。最主要的是我不确定如何在点击#done_edit_slug时结束stringToSlug()函数。

谢谢!

+0

return false; ? – delphist 2011-02-27 00:08:01

回答

1

在您完成的事件处理程序中,尝试解除文本框上的事件绑定。

$('#done_edit_slug').live('click', function() { 
     $("#edit_project_name").unbind("keyup keydown blur"); 
     $('input.project_slug').after("<strong><span>"+$('input.project_slug').val()+"</span></strong>"); 
     $('input.project_slug').hide(); 
     $('#done_edit_slug').remove().end(); 
     $('#edit_slug').show(); 
}); 

这是在假设您使用插件源中的默认事件的情况下完成的。

+0

Welp,很简单!谢谢,我对jQuery还比较陌生。 – BandonRandon 2011-02-27 00:15:31