make a input field required if radio is checked

前端 未结 3 1183
礼貌的吻别
礼貌的吻别 2021-01-01 23:53

Can I somehow insert the required attribute into an input field only if a certain radio is checked?

I have an input field named \"ladder-meters\" that is not require

3条回答
  •  醉酒成梦
    2021-01-01 23:58

    Easy to achieve with jQuery:

    $('#ladder').change(function () {
        if($(this).is(':checked') {
            $('#ladder-meters').attr('required');
        } else {
            $('#ladder-meters').removeAttr('required');
        }
    });
    

提交回复
热议问题