请教一下Jquery的unbind 和 bind函数用法

图片描述

formValidate: function(){
        $("input[type='image']").unbind("click");
        $("input[type='image']").bind("click", function(){
            if ($("select[name='did'] option:selected").attr("value") == "") {
                alert("请选择部门");
                return false;
            }
            else {
                if (!$("select[name='pids'] option:selected").attr("value")) {
                    alert("请选择岗位");
                    return false;
                }else{
                    if($("#message").text()=="该用户名已经存在"){
                        alert("请输入合理的用户名");
                        return false;
                    }else{
                        return true;    
                    }
                }
                return true;
            }
        });
阅读 2.8k
2 个回答

本质上就是on和off的一个快捷方法

直接看源码吧
https://github.com/jquery/jqu...

    bind: function( types, data, fn ) {
        return this.on( types, null, data, fn );
    },
    unbind: function( types, fn ) {
        return this.off( types, null, fn );
    },
  • input[type='image'] 是一种jquery的一种选择器,属性选择器文档可以参考下面文档http://jquery.cuishifeng.cn/attributeEquals.html

  • unbind/bind 是事件解绑/绑定操作,可以直接bind,需不需要先unbind要根据你的业务代码决定。
    高版本jquery,建议统一事件api,使用on/off代替