简体   繁体   中英

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? Does "submit" button omit non-displaying field automatically? Do I need add on further javascript to make it only required when appearing?

Does "submit" button omit non-displaying field automatically? Do I need add on further javascript to make it only required when appearing?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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