如何根据页面加载时选中的单选按钮禁用文本区域?

时间:2015-04-29 07:35:16

标签: javascript jquery radio-button

我希望通过单选按钮选择来改进此script以禁用文本区域。当前选择“是”时,如果选择“否”,则应禁用并启用文本区域。

我需要更改哪些内容,以便在首次加载页面时以及选中正确的单选按钮时禁用文本区域?(如果未进行任何更改,则默认选中“是”)

请记住,此页面将被刷新并重定向到,因此我需要检查单选按钮上的实际选择。我只是将它们设置为在加载之前被禁用,但是即使我选择了正确的单选按钮,这个禁用的文本区域也是如此。

HTML

request

使用Javascript / JQuery的

<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>

希望足够清楚。

2 个答案:

答案 0 :(得分:2)

$(".group").find(":radio[value=yes]:checked").each(function () {
    $('[data-trigger="' + $(this).attr("name") + '"]').prop('readonly', true).css('background-color', '#EBEBE4');
});

<强> DEMO

答案 1 :(得分:0)

将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>
在你的脚本中

: -

$( document ).ready(function() {
 $("textarea.group").attr("disabled", true);
});