javascript - 一个表单验证的问题,如何通过获取当前校验得文本框ID进而使用相应JSON键值的问题,非常感谢
阿神
阿神 2017-04-10 15:56:19
[JavaScript讨论组]

手机: 提示文字

function checkAction(){ this.addStyle = function(){ $(this).addClass("focus"); }; this.removeStyle = function(){ $(this).removeClass("focus"); } } var checkItem = { mobile : { reg : "/^1[3|5][0-9]\d{4,8}$/", error: "手机号格式错误", tip:"请输入手机号用户密码找回" }, email:{ reg:"", error:"邮件格式错误", tip:"请输入邮件地址" } }; $(function(){ // 手机验证 $('input[type=text]').bind('focus', function () { checkAction.call(this); this.addStyle(); }); $('input[type=text]').bind('blur', function () { checkAction.call(this); this.removeStyle(); var getId = this.id;//获得当前文本框ID if ( this.value == '' ){ $(this).next('span').css('display','block'); } else { if( !(checkItem.getId.reg.test(this.value)) ){//我的问题是:通过checkItem.mobile.reg可以读到mobile的json字典表的值,那么这里我想动态的判断是mobile这个ID表单项,进而通过checkItem.getId.reg也同样能读到mobile相应的键值,有劳大牛指导,谢谢 alert(checkItem.mobile.error); } else { alert("输入正确"); } } }); });
阿神
阿神

闭关修行中......

全部回复(1)
怪我咯

checkItem.getId.reg.test(this.value)这句有问题
改成
checkItem[getId].reg.test(this.value)就可以了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号