简体   繁体   English

如何创建需要填写的条件字段

[英]How to create conditional fields that are required to be filled out

I learned from other post that I could use the following the create a conditional field: 我从其他帖子中学到,可以使用以下内容创建条件字段:

$('your selects class or id').change(function(){    
    if($(this).val() == "publish_on"){
        $('.secret').fadeIn();
    }
    else{
        $('.secret').hide();
    }
});

From my understanding of this code, it only show/hides the field, instead of actually cancel the field. 根据我对这段代码的理解,它仅显示/隐藏字段,而不实际取消该字段。 This is all fine except what if the pop-up field (.secret) needs to be a required field and I use form validation to make sure all required fields are filled out? 一切都很好,但是如果弹出字段(.secret)需要为必填字段,并且我使用表单验证来确保所有必填字段都已填写怎么办? Does "submit" button omit non-displaying field automatically? “提交”按钮是否会自动忽略非显示字段? Do I need add on further javascript to make it only required when appearing? 我是否需要添加其他JavaScript以使其仅在出现时才需要?

Does "submit" button omit non-displaying field automatically? “提交”按钮是否会自动忽略非显示字段? Do I need add on further javascript to make it only required when appearing? 我是否需要添加其他JavaScript以使其仅在出现时才需要?

No,It doesn't omit the hidden fields.It submit's them too. 不,它不会忽略隐藏的字段。它也会提交它们。

And you need to omit them while doing validation. 并且在验证时您需要省略它们。

Some thing like using visible seletors 诸如使用可见的筛选器之类的东西

if ( $("#targetEleme").is(":visible") ) {

//then do this validation
}

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

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