[英]How do I disable text areas based on what radio button is checked when the page loads?
I want to improve upon this script for disabling text areas by radio button selection. 我想改进此脚本,以通过单选按钮选择禁用文本区域。 Currently when "yes" is selected the text areas should be disabled and enabled when "no" is selected.
当前,当选择“是”时,应在选择“否”时禁用并启用文本区域。
What do I need to change so the text areas are disabled when the page first loads and if the correct radio button is checked?(yes is checked by default if no change has been made) 我需要更改什么,以便在首次加载页面时选中文本区域,并且是否选中了正确的单选按钮?(如果未进行任何更改,则默认选中是)
Please bare in mind this page will be refreshed and redirected to so I will need to check the actual selection on the radio buttons. 请记住,此页面将刷新并重定向到,因此我将需要检查单选按钮上的实际选择。 I was just setting them to all be disabled on load before but this disabled text areas even when I had the correct radio button selected.
我只是将它们设置为在加载之前全部禁用,但是即使我选择了正确的单选按钮,这也会禁用文本区域。
HTML 的HTML
<div class="group">
<input type="radio" name="choice1" value="yes" checked/>Yes
<input type="radio" name="choice1" value="no" />No
</div>
<div class="group">
<input type="radio" name="choice2" value="yes" checked/>Yes
<input type="radio" name="choice2" value="no" />No
</div>
<div class="group">
<input type="radio" name="choice3" value="yes" checked/>Yes
<input type="radio" name="choice3" value="no" />No
</div>
<div>
<textarea data-trigger="choice1" rows="4" cols="20"></textarea>
<textarea data-trigger="choice2" rows="4" cols="20"></textarea>
<textarea data-trigger="choice3" rows="4" cols="20"></textarea>
</div>
Javascript/JQuery Javascript / JQuery
$(function () {
var $choices = $(".group").find(":radio");
$choices.on("change", function () {
var $this = $(this);
var choiceName = $this.attr('name');
var tarea = $('[data-trigger="' + choiceName + '"]');
if ($this.val() === "yes") {
tarea.val('');
tarea.prop('readonly', true);
tarea.css('background-color', '#EBEBE4');
} else {
tarea.prop('readonly', false);
tarea.css('background-color', '#FFFFFF');
}
});
});
Hope that's clear enough. 希望这很清楚。
Add the class attribute to your textarea 将class属性添加到您的textarea
<div>
<textarea data-trigger="choice1" rows="4" cols="20" class="group"></textarea>
<textarea data-trigger="choice2" rows="4" cols="20" class="group"></textarea>
<textarea data-trigger="choice3" rows="4" cols="20" class="group"></textarea>
</div>
in your script:- 在您的脚本中:-
$( document ).ready(function() {
$("textarea.group").attr("disabled", true);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.