set bootstrap "required" attribute on textarea only if specific radio button selected

set bootstrap "required" attribute on textarea only if specific radio button selected

这是我第一次post来这里,我对这一切都是业余的,所以请多多包涵。

我确实根据单选按钮选择显示带有 Jquery 的文本区域(3 个单选按钮,所以 3 个假设的文本区域)。

我确实在它们上设置了 bootstrap "required" 属性,但是由于只显示了 3 个中的 1 个,我无法验证表单,因为其他 2 个框是空的...

知道如何仅在显示的(活动的)文本区域上使用 Bootstrap 或 Jquery 启用此属性吗?

感谢您的帮助。


代码如下:

<div class="form-row">
    <div class="col-md-12 mb-3">
        <label for="type_demande">Request type</label>

        <div class="custom-control custom-radio form-control-sm">
            <input type="radio" id="besoin_hw_remplacement" name="request_type" class="custom-control-input" value="hw_remplacement" required="" />
            <label class="custom-control-label" for="besoin_hw_remplacement">Replace X</label>
        </div>
        <div class="custom-control custom-radio form-control-sm">
            <input type="radio" id="besoin_sw_remplacement" name="request_type" class="custom-control-input" value="sw_remplacement" required="" />
            <label class="custom-control-label" for="besoin_sw_remplacement">Replace Y</label>
        </div>                      
    </div>
</div>  

<div class="form-group" id="hw_remplacement_details" style="display:none">
    <label for="details_projet">Replace X</label>
    <textarea class="form-control form-control-sm" id="details_projet" name="details_projet" rows="4" placeholder="Project Details X..." required="" /></textarea>
    <div class="invalid-feedback">
        Please give details
    </div>
</div>

<div class="form-group" id="sw_remplacement_details" style="display:none">
    <label for="details_projet">Replace Y</label>
    <textarea class="form-control form-control-sm" id="details_projet" name="details_projet" rows="4" placeholder="Project Details Y" required="" /></textarea>
    <div class="invalid-feedback">
        Please give details
    </div>
</div>  

<script type="text/javascript">
    $("input[name='request_type']:radio")
    .change(function() {
        $("#hw_remplacement_details").toggle($(this).val() == "hw_remplacement");
        $("#sw_remplacement_details").toggle($(this).val() == "sw_remplacement"); 
});
</script>   

如果您使用 jquery 验证插件那么 你使用 "ignore"

$("#form").validate({
   ignore: ":hidden"
});
<!DOCTYPE html>
<html>
<head>
    <title>Javascript test</title>
</head>
<body>
    <label><input name="option" type="radio" value="1"> Option 1</label>
    <label><input name="option" type="radio" value="2"> Option 2</label>
    <label><input name="option" type="radio" value="3"> Option 3</label>
    <hr/>
    <div >
        <textarea class="textarea" id="textarea-1" placeholder="textarea 1" ></textarea>
    </div>
    <div >
        <textarea class="textarea" id="textarea-2" class="textarea" placeholder="textarea 2"></textarea>
    </div>
    <div>
        <textarea class="textarea" id="textarea-3" class="textarea" placeholder="textarea 3"></textarea>
    </div>
</body>
<script type="text/javascript">
    var options=document.querySelectorAll("[name=option]");
    console.log(options.length);
    for(var i=0;i<options.length;i++)
    {
        options[i].addEventListener("change",updateTextArea);
    }

    function updateTextArea()
    {
        var all_textarea=document.getElementsByClassName("textarea");
        for(var i=0;i<all_textarea.length;i++)
        {
            all_textarea[i].style.display='none';
            all_textarea[i].removeAttribute("required");
        }
        var value=document.querySelectorAll("[name=option]:checked")[0].value;
        console.log(document.getElementById("textarea-1").style.display);
        if(value==1)
        {
            document.getElementById("textarea-1").style.display='block';
            document.getElementById("textarea-1").setAttribute("required","required");

        }
        else if(value==2)
        {
            document.getElementById("textarea-2").style.display='block';
            document.getElementById("textarea-2").setAttribute("required","required");
        }
        else if(value==3)
        {
            document.getElementById("textarea-3").style.display='block';
            document.getElementById("textarea-3").setAttribute("required","required");
        }
    }
</script>
</html>

这是一个解决方案,根据您的代码进行了调整:

    $("input[name='request_type']:radio")
    .change(function() {
     $("#details_projet").prop("required", false);
        $("#hw_remplacement_details").toggle($(this).val() == "hw_remplacement");
        $("#sw_remplacement_details").toggle($(this).val() == "sw_remplacement");
        $("#"+$(this).val()+"_details #details_projet").prop('required',true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-row">
    <div class="col-md-12 mb-3">
        <label for="type_demande">Request type</label>

        <div class="custom-control custom-radio form-control-sm">
            <input type="radio" id="besoin_hw_remplacement" name="request_type" class="custom-control-input" value="hw_remplacement" required="" />
            <label class="custom-control-label" for="besoin_hw_remplacement">Replace X</label>
        </div>
        <div class="custom-control custom-radio form-control-sm">
            <input type="radio" id="besoin_sw_remplacement" name="request_type" class="custom-control-input" value="sw_remplacement" required="" />
            <label class="custom-control-label" for="besoin_sw_remplacement">Replace Y</label>
        </div>                      
    </div>
</div>  

<div class="form-group" id="hw_remplacement_details" style="display:none">
    <label for="details_projet">Replace X</label>
    <textarea class="form-control form-control-sm" id="details_projet" name="details_projet" rows="4" placeholder="Project Details X..." /></textarea>
    <div class="invalid-feedback">
        Please give details
    </div>
</div>

<div class="form-group" id="sw_remplacement_details" style="display:none">
    <label for="details_projet">Replace Y</label>
    <textarea class="form-control form-control-sm" id="details_projet" name="details_projet" rows="4" placeholder="Project Details Y" /></textarea>
    <div class="invalid-feedback">
        Please give details
    </div>
</div>

有几处变化:

  1. 从两个 <textarea> 标签中删除 required=""
  2. $("#details_projet").prop("required", false);添加到变化函数的开头
  3. 在更改功能的底部添加$("#"+$(this).val()+"_details #details_projet").prop('required',true);