jquery的一些简单用法

复习一下jquery获取表单的一些用法:

表单是动态生成的一个数组,可以有N个class和name相同的表单

div代码:

<textarea rows="" cols="" name="monitorShowUriKey" class="form-control input-sm monitor" style="resize: none;width:180px">${fn:escapeXml(extralist.url)}</textarea>
										

提交按钮的click事件里面做判断逻辑:

$(".monitor").each(function(){
			var val = $.trim($(this).val());
			if(val.length==0){
				$(this).next().html("不能为空");
				$(this).closest("div.form-group").addClass("has-error");
				validateSuccess = false;
			}else if(val.length>500){
				$(this).next().html("长度不能超过500");
				$(this).closest("div.form-group").addClass("has-error");
				validateSuccess = false;
			}else{
				$(this).next().html("");
				$(this).closest("div.form-group").removeClass("has-error");
			}
		});

实际上就是jquery的each的用法,重点关注class的写法,monitor

 

想添加一个输入区域失去焦点的事件,类似于onmouseout,jquery里面的实现是blur方法,

但是,单个表单好实现,这种name为数组的情况,应该如何实现呢?用live

具体实现如下:

$(".monitor").live("blur",(function(){
		var val = $(this).context.value;
		if(val.length==0){
			$(this).next().html("不能为空");
			$(this).closest("div.form-group").addClass("has-error");
			validateSuccess = false;
		}else if(val.length>500){
			$(this).next().html("长度不能超过500");
			$(this).closest("div.form-group").addClass("has-error");
			validateSuccess = false;
		}else{
			$(this).next().html("");
			$(this).closest("div.form-group").removeClass("has-error");
		}
	}));

注意,这个得写在$(document).ready(function(){里面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值