jquery validate attr("name")总是提示undefined

sspanzervor1 2019-01-07 08:56:09
如题。因为希望自定义的Checkbox的errorplacement在其它位置,所以Javascript里面做了个判断,但后来发现所有的attr("name")的值均为undefined,百思不得其解,盼请各位前辈高手之间,不胜感激!!



<form class="FT_form" id="frm1" name="frm1" method="post">
<li>
<label for="lbl_cname">* 客户姓名(必填):</label>
<input type="text" id="txt_cname" name="txt_cname">
</li>
<li>
<label for="lbl_ctel">* 客户联系电话(必填):</label>
<input type="text" id="txt_ctel" name="txt_ctel">
<!--<label for="txt_cname"></label>-->
</li>
<li>
<label for="lbl_cMRN"> MRN编号(选填):</label>
<input type="text" id="txt_cMRN" name="txt_cMRN">
<!--<label for="txt_cMRN"></label>-->
</li>

<li>
<label for="lbl_action">* 房间登记类别(必选):</label>
<div class="cltype_chk">
<input type="checkbox" checked="" id="chk_type" value="2" name="chk_type" style="width:20px;">
<label for="chk_ordered" class="chk_lbl">预定</label>
<input type="checkbox" id="chk_type" value="3" name="chk_type" style="width:20px;">
<label for="chk_in" id="chk_in1" class="chk_lbl">入住</label>
</div>
<div id="error_action" >error:</div>
</li>

</form>




<script src="jquery-ui-1.9.2.custom/js/jquery-1.8.3.js"></script>
<script src="jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="jquery-ui-1.9.2.custom/development-bundle/ui/jquery.ui.datepicker.js"></script>
<script src="jquery/dist/jquery.validate.js"></script>
<script src="jquery/dist/jquery.validate.min.js"></script>
<script src="jquery/dist/localization/messages_zh.js"></script>
<script src="jquery/jquery_validate_extended.js"></script>

<script>
$("#frm1").validate({
errorElement:'span',

rules: {
slt_rnum: {required:true},
txt_cname: {required:true, maxlength:30},
txt_ctel: {required: true, isMobile: true},
chk_type: {required: true}
},
messages: {
slt_rnum: {required:"请选择房型和房号!"},
txt_cname: {required:"客户姓名不能为空", maxlength: "姓名不能超过30字符"},
txt_ctel: {required: "客户手机号为必填项!", isMobile:"手机号码应为11位数字!"},
chk_type: {required:"AAA"}
},

errorPlacement: function(error, element) {
if (error.attr("name")=="chk_type")
{//alert("aaa");
error.appendTo("#error_action");}
else
{error.appendTo(element.parent());
alert(element.attr('tagname'));}
},
}) ;
</script>
...全文
538 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hello World, 2019-01-10
  • 打赏
  • 举报
回复
你要找的东西应该是element,error是错误提示信息的元素,用tagName或者用type来判断类型
jonisen 2019-01-07
  • 打赏
  • 举报
回复
用下面的方法可以获取到CheckBox元素,并且element.attr('tagname')这个要大写element.attr('tagName') element.attr("name") "chk_type"

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧