HTML5 required attribute one of two fields

前端 未结 4 1699
刺人心
刺人心 2021-02-05 03:26

I have a form with two required input fields:

4条回答
  •  北海茫月
    2021-02-05 04:10

    For two text fields @Andy's answer is working awesome, but in case of more than two fields we can use something like this.

    jQuery(function ($) {
        var $inputs = $('input[name=phone],input[name=mobile],input[name=email]');
        $inputs.on('input', function () {
            var total = $('input[name=phone]').val().length + $('input[name=mobile]').val().length + $('input[name=email]').val().length;
            $inputs.not(this).prop('required', !total);
    
        });
    });
    

提交回复
热议问题