如何从 jquery 获取输入类型电子邮件的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18442829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:38:14  来源:igfitidea点击:

how to get input type email's value from jquery

jqueryselector

提问by Mohammad Faisal

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

我试图<input type="email" name="mail">jQuery.
我必须运行验证以检查mail的值

<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 mailblank.
I've gone through jQuery API Docbut not found any selectorfor email. Why so?
Why it is not present there and how would I solve this problem?

但如果用户mail留空,表单仍在提交。
我已经经历了jQuery的API文档,但没有发现任何selectoremail。为什么这样?
为什么它不存在那里,我将如何解决这个问题?

采纳答案by Sudhir Bastakoti

you could do:

你可以这样做:

$.trim($("input[name='mail']").val());

回答by Troels M. B. Jensen

I see that this is rather old, but I would like to throw my two cents in for the future generations.

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

While there is not strictly a pseudo selector like input:email, there is a type-selector version that works just like the pseudo would: input[type=email]

虽然严格来说没有像 那样的伪选择器input:email,但有一个类型选择器版本可以像伪选择器一样工作:input[type=email]

Extra reading:

额外阅读:

this selector can do alot of cool things like selecting all that starts with an id: input[id^=prod-]would select all inputs with an id of prod-X where X could be anything and any length. This is achieved using ^=

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

回答by revoke

You can extend jQuery by this easy function http://markdalgleish.com/2011/05/jquery-selector-for-html5-input-types/After this you can select all text inputs by calling $('input:textall');

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

回答by Junaid Asmat

Use this code to get date submitted through form, this will work perfectly fine:

使用此代码获取通过表单提交的日期,这将工作得很好:

$("input[type = 'date']").val();