这是我在这里发表的第一篇文章之一,我是业余爱好者,所以请耐心等待。
我根据单选按钮的选择显示一个带有Jquery的textarea(3个单选按钮,所以3个假设的textarea)。
我设置了引导程序" required"属性,但是,因为只显示3中的1,我无法验证表格,因为其他2个方框是空的......
是否知道如何仅在显示的(活动的)textarea上使用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>
答案 0 :(得分:1)
这是一个解决方案,使用您的代码进行调整:
$("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>
有几处变化:
required=""
个标记<textarea>
$("#details_projet").prop("required", false);
添加到更改功能的开头$("#"+$(this).val()+"_details #details_projet").prop('required',true);
添加到更改功能的底部答案 1 :(得分:0)
如果您正在使用jquery验证插件 你使用“忽略”
$("#form").validate({
ignore: ":hidden"
});
答案 2 :(得分:0)
<!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>