jquery 必填项判断表单是否为空的方法
更新时间:2008年09月14日 20:42:33 作者:
jquery 表单验证,检测表单是否为空
html页面
<form onsubmit="if(confirm('确定好你所填写的正确,不然会发错!')) {return checkForm();}else{return false;}" >
<table >
<tr>
<td>选择发送人<font class="red">*</font></td>
<td>
<input type="text" name="sendto_type[]" id="sendto_type1" value="1" >按城市发送
</td>
</tr>
<tr>
<td>选择发送人2<font class="red">*</font></td>
<td>
<input type="text" name="sendto_type[]" id="sendto_type1" value="1" >按城市发送
</td>
</tr>
</table>
</form>
js代码
<script>
function checkForm() {
pass = true;
$("td:contains('*')").next().find("input").each(function(){
if(this.value == '') {
text = $(this).parent().prev().text();
alert(text+"是必填项");
this.focus();
pass = false;
return false;//跳出each
}
});
return pass;
}
</script>
复制代码 代码如下:
<form onsubmit="if(confirm('确定好你所填写的正确,不然会发错!')) {return checkForm();}else{return false;}" >
<table >
<tr>
<td>选择发送人<font class="red">*</font></td>
<td>
<input type="text" name="sendto_type[]" id="sendto_type1" value="1" >按城市发送
</td>
</tr>
<tr>
<td>选择发送人2<font class="red">*</font></td>
<td>
<input type="text" name="sendto_type[]" id="sendto_type1" value="1" >按城市发送
</td>
</tr>
</table>
</form>
js代码
复制代码 代码如下:
<script>
function checkForm() {
pass = true;
$("td:contains('*')").next().find("input").each(function(){
if(this.value == '') {
text = $(this).parent().prev().text();
alert(text+"是必填项");
this.focus();
pass = false;
return false;//跳出each
}
});
return pass;
}
</script>
相关文章
jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证
这篇文章主要介绍了jQuery实现按钮的点击 全选/反选 单选框/复选框 文本框 表单验证的相关资料,需要的朋友可以参考下2015-06-06
jQuery v3.3.1的BUG以及解决办法(附解决方案)
这篇文章描述了我们 FineUIPro 产品 更新中遇到的一个问题,最终将问题定位到 jQuery.position() 函数,虽然jQuery的做法是依照HTML规范来的,但是 jQuery.offsetParent() 和 jQuery.position() 两个函数有冲突,并且会导致之前的jQuery插件出错,应该算是一个BUG吧2023-03-03


最新评论