简体   繁体   English

当表单与jQuery插件混淆时,如何检查表单的有效性?

[英]How to check validity of form when it messes with a jQuery plugin?

I have a normal form with some textboess that are required. 我有一个带有一些必需文本的标准表格。 So when these textboxes are not filled, the HTML5 shows that warning in each field. 因此,当未填充这些文本框时,HTML5将在每个字段中显示该警告。

What I wanted to do is to show a nice notification when everything is filled. 我想做的是在所有内容填充后显示一个不错的通知。 And it is working correctly, but id does not check if the textboxes are filled.. 并且它可以正常工作,但是id不会检查文本框是否已填充。

document.querySelector('#submit1').onclick = function(e){
e.preventDefault();
    swal("Thank you!", "Your request has been submitted.", "success");
};

I took it a step forward and it checks the inputs before submitting, but the popup does not show up now. 我向前迈了一步,它在提交之前检查了输入,但是现在不显示弹出窗口。 It takes me to the same page. 它带我到同一页面。

document.querySelector('#submit1').onclick = function(e){
    e.preventDefault();       
    if (if ($('#contact-form')[0].checkValidity()) {
    swal("Thank you!", "Your request has been submitted.", "success");
     }
    return;
};

Is there any way to fix this? 有没有什么办法解决这一问题?

Thank you 谢谢

<form id="contact-form" action="" name="contactform" class="row" method="post" >
<input required type="text" name="first_name" id="name"> 
<input required type="password" name="first_name" id="name"> 
<input type="submit" id="submit1">
</form>

Whats the extra if in the function 如果在函数中还有什么额外的

if( if ($('#contact-form')[0].checkValidity()) { if( if($('#contact-form')[0] .checkValidity()){

It should be: 它应该是:

if ($('#contact-form').checkValidity()) {
    swal("Thank you!", "Your request has been submitted.", "success");
}

There are some assumptions I am making here: 1. you have checkValidity() as some method attached to your form. 我在这里有一些假设:1.您有checkValidity()作为附加到表单的某种方法。 2. swal is some custom function 2. swal是一些自定义功能

Also, you can use 另外,您可以使用

$('#submit1').on('click', function(e) {
});

or 要么

$('#submit1').click(function(e) {
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM