5

I was trying to get the <input type="email" name="mail">'s value from jQuery.
I've to run a validation for checking mail's value

<script>
    function valid(){
        var em = $.trim($('input:email[name=mail]').val());
        if(em.length < 5){
            alert('Please enter email id');
            return false;
        }
        return true;
    }
</script>

but the form is still submitting if the user left mail blank.
I've gone through jQuery API Doc but not found any selector for email. Why so?
Why it is not present there and how would I solve this problem?

4

5 回答 5

8

我看到这是相当古老的,但我想为后代投入我的两分钱。

虽然没有严格意义上的伪选择器input:email,但有一个类型选择器版本,其工作方式与伪选择器类似:input[type=email]

额外阅读:

这个选择器可以做很多很酷的事情,比如选择所有以 id 开头的东西:input[id^=prod-]将选择 id 为 prod-X 的所有输入,其中 X 可以是任何长度。这是使用 ^= 实现的

于 2017-05-16T06:54:55.320 回答
3

你可以这样做:

$.trim($("input[name='mail']").val());
于 2013-08-26T11:17:25.033 回答
1

万一它对任何人有帮助,我有两个具有相同 ID 的输入,但一个用于移动设备,一个用于桌面。我正在测试桌面并没有得到任何价值。结果发现移动设备虽然隐藏,但输入被选中,因此返回空白。

于 2020-11-07T11:24:49.000 回答
1

使用此代码获取通过表单提交的日期,这将非常有效:

$("input[type = 'date']").val();
于 2019-06-28T06:23:35.147 回答
0

您可以通过这个简单的功能扩展jQuery http://markdalgleish.com/2011/05/jquery-selector-for-html5-input-types/在此之后您可以通过调用选择所有文本输入$('input:textall');

于 2016-07-01T15:57:49.560 回答