简体   繁体   English

如果未选中元素,则需要帮助防止表单提交

[英]Need help preventing form submission if element is not checked

I wrote a small script the prevents the form from being submitted if an input field (checkbox or radio) has not been selected. 我编写了一个小脚本,以防止在未选择输入字段(复选框或单选框)的情况下提交表单。 The script only targets the input field with the attribute "required". 该脚本仅将属性为“ required”的输入字段作为目标。 However since my form varies from page to page (depending on what link the user selects) not all options required. 但是,由于我的表单因页面而异(取决于用户选择的链接),因此并非所有选项都需要。

The form that I am including the jquery script is on one form since it is dynamic. 我包含的jquery脚本的形式是一种形式,因为它是动态的。

I have wrote the script below which does the job 我写了下面的脚本来完成工作

  $('#item_form').submit(function() {
            var ok = $('input[id*=\"required\"]').is(':checked');
            $('#error').toggle(!ok);
            $('html, body').animate({scrollTop: 0,scrollLeft: 300}, 500);
            return ok;
        });
    });

But the challenge that I am facing having it work only if there is a required attribute. 但是我面临的挑战只有在具有必需属性的情况下才能起作用。 I have wrote the following code below but it doesn't seem to be working for me. 我在下面编写了以下代码,但它似乎不适用于我。 Here is what I wrote 这是我写的

   $.fn.exists = function () {
        return this.length !== 0;
    }

        $('#item_form').submit(function() {
            var req = $('input[id*=\"required\"]');
            if(req.exists()){
            var ok =  req.is(':checked');
            $('#error').toggle(!ok);
            $('html, body').animate({scrollTop: 0,scrollLeft: 300}, 500);
            return ok;
            }
        });
    });

Can anyone please help me figure this one out? 谁能帮我解决这个问题? Thanks! 谢谢!

It isn't entirely clear what your validation requirements are but if you are trying to target required attribute you can use an attribute selector' 尚不清楚您的验证要求是什么,但是如果您要定位required属性,则可以使用属性选择器'

For example on a single checkbox: 例如,在一个复选框上:

<input type="checkbox" required/>
var ok = $(':checkbox[required]').is(':checked');

Would need to see more html and have better explanation of your needs to improve this 需要查看更多的html并更好地解释您的需求以改善这一点

See API Docs for has-attribute selector 有关具有属性选择器的信息,请参见API文档

You can try to check if the selector input[id*="required"]:not(:checked) returns with length "0", that will be true either if all required are checked, and also if there's no required on the page: 您可以尝试检查选择器input[id*="required"]:not(:checked)以长度“ 0”返回,如果选中了所有required ,并且页面上没有required ,则返回true

$('#item_form').submit(function() {
    var ok = $('input[id*=\"required\"]:not(:checked)').length == 0;
    $('#error').toggle(!ok);
    $('html, body').animate({scrollTop: 0,scrollLeft: 300}, 500);
    return ok;
});

Simple solution... 简单的解决方案...

$('#item_form').submit(function () {
   if ($(this).find('input.required').length > 0) {
       alert('oops, a field is required'); // this is where you'd put your loop to make sure each required field is filled
   } else {
       alert('form is good to go');
   }
});

Note though that you can only have one instance of an ID on a page, so use class="required" instead of id="required". 请注意,尽管页面上只能有一个ID实例,所以请使用class =“ required”而不是id =“ required”。

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

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