jQuery:查找所有可见的必填字段 [英] jQuery: Find all the visible required fields

查看:306
本文介绍了jQuery:查找所有可见的必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找所有具有required属性的字段,它们也应为visible.因为页面也可以隐藏必填字段.这是我尝试过的:

I am trying to find all the fields with required attribute and they should be visible too. Because page can have hidden required fields too. Here is what I tried:

function validateRequiredFields()
{
    $('input,textarea,select').attr('required',true).filter(':visible:first').each(function(i, requiredField){

        if($(requiredField).val()=='')
        {
            alert($(requiredField).attr('name'));
        }
    });
}

推荐答案

如果要查找输入,文本区域或选择具有属性required且为visible的元素,请使用

If you want to find input, textarea,or select elements that have the attribute required and are visible use the has attribute selector:

$('input,textarea,select').filter('[required]:visible')

$(':input[required]:visible')//might be little costlier

这篇关于jQuery:查找所有可见的必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆