bootstrap table:JQuery中each方法绑定blur事件监听input输入是否合法,进入死循环

一、问题描述:bootstrap table中每行有一个input框,要对每个input进行检查数值是否在1~10之间(可保留小数点后一位)的数值,每次bind事件之前没有解绑事件,造成点击alert事件次数逐次递增,最后死循环

二、解决方案:每次绑定事件之前进行解绑

//判断输入值为1与10之间,可保留小数点一位
            $("input[name='SelfScoreA']").each(function () {
                var binded = $(this).attr("blurbinded");  //标记是否有绑定
                if(binded){return ;} //有绑定直接返回
                $(this).unbind("blur"); //解绑
                $(this).bind('blur', function () {
                    $(this).attr("blurbinded","binded");//每次触发事件进行标记
                    if ($.trim($(this).val()) != "") {
                        if (isNaN($(this).val())) {
                            alert('请填写1~10分!');
                        }
                        else if (/^((0\.[1-9]{1})|(([1-9]{1})(\.\d{1})?)|10)$/.test($(this).val()) == false) {
                            alert('请填写1~10分!');
                        }
                        else if ($(this).val() > 10 || $(this).val() <= 0) {
                            alert('请填写1~10分!');
                        }else{
                            row.score = $(this).val();
                        }
                    }
                });
            });

猜你喜欢

转载自www.cnblogs.com/CherishZeng/p/10963404.html