admin管理员组文章数量:1023230
Whenever I place a $(".Othertext").attr('required', '');
before the show call for the element it shows the textbox regardless of the button condition. Is there any way to make it so that the textbox is required and shown when the Other button is clicked?
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect" for="DF4">
<input type="radio" id="DF4" class="mdl-radio__button" name="DF" value="4">
<span class="mdl-radio__label">Other - please describe in detail</span>
</label>
<div class="Othertext">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="othertext">
<span class="mdl-textfield__label">Describe...</span>
</div>
</div>
<script src=".2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".Othertext").hide();
$('input[type=radio][name=DF]').change(function() {
if($(this).val() == 4)
$(".Othertext").show();
else
$(".Othertext").hide();
});
});
</script>
Whenever I place a $(".Othertext").attr('required', '');
before the show call for the element it shows the textbox regardless of the button condition. Is there any way to make it so that the textbox is required and shown when the Other button is clicked?
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect" for="DF4">
<input type="radio" id="DF4" class="mdl-radio__button" name="DF" value="4">
<span class="mdl-radio__label">Other - please describe in detail</span>
</label>
<div class="Othertext">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="othertext">
<span class="mdl-textfield__label">Describe...</span>
</div>
</div>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".Othertext").hide();
$('input[type=radio][name=DF]').change(function() {
if($(this).val() == 4)
$(".Othertext").show();
else
$(".Othertext").hide();
});
});
</script>
Share
Improve this question
edited Jun 19, 2017 at 18:57
epictaco24
asked Jun 19, 2017 at 18:22
epictaco24epictaco24
812 silver badges11 bronze badges
1 Answer
Reset to default 3This works fine for me:
$(document).ready(function() {
$("#othertext").hide();
$('input[type=radio][name=DF]').change(function() {
if($(this).val() == 4) {
$("#othertext").show();
$("#othertext").attr('required', '');
}
else {
$("#othertext").hide();
$("#othertext").removeAttr('required', '');
}
});
});
However remember to use brackets when you have more then 1 line in the if statement.
Whenever I place a $(".Othertext").attr('required', '');
before the show call for the element it shows the textbox regardless of the button condition. Is there any way to make it so that the textbox is required and shown when the Other button is clicked?
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect" for="DF4">
<input type="radio" id="DF4" class="mdl-radio__button" name="DF" value="4">
<span class="mdl-radio__label">Other - please describe in detail</span>
</label>
<div class="Othertext">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="othertext">
<span class="mdl-textfield__label">Describe...</span>
</div>
</div>
<script src=".2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".Othertext").hide();
$('input[type=radio][name=DF]').change(function() {
if($(this).val() == 4)
$(".Othertext").show();
else
$(".Othertext").hide();
});
});
</script>
Whenever I place a $(".Othertext").attr('required', '');
before the show call for the element it shows the textbox regardless of the button condition. Is there any way to make it so that the textbox is required and shown when the Other button is clicked?
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect" for="DF4">
<input type="radio" id="DF4" class="mdl-radio__button" name="DF" value="4">
<span class="mdl-radio__label">Other - please describe in detail</span>
</label>
<div class="Othertext">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="othertext">
<span class="mdl-textfield__label">Describe...</span>
</div>
</div>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".Othertext").hide();
$('input[type=radio][name=DF]').change(function() {
if($(this).val() == 4)
$(".Othertext").show();
else
$(".Othertext").hide();
});
});
</script>
Share
Improve this question
edited Jun 19, 2017 at 18:57
epictaco24
asked Jun 19, 2017 at 18:22
epictaco24epictaco24
812 silver badges11 bronze badges
1 Answer
Reset to default 3This works fine for me:
$(document).ready(function() {
$("#othertext").hide();
$('input[type=radio][name=DF]').change(function() {
if($(this).val() == 4) {
$("#othertext").show();
$("#othertext").attr('required', '');
}
else {
$("#othertext").hide();
$("#othertext").removeAttr('required', '');
}
});
});
However remember to use brackets when you have more then 1 line in the if statement.
本文标签: javascriptHow to conditionally set required attribute when element is not hiddenStack Overflow
版权声明:本文标题:javascript - How to conditionally set required attribute when element is not hidden - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745582352a2157384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论