-3

我有两个单选按钮。如果单击第一个单选按钮,则每个单选按钮都有两个文本框,然后应该禁用包含文本框的其他单选按钮,反之亦然。

4

3 回答 3

4

您可以使用 jquery click 事件,结合 :checked 过滤器

试试这个小提琴http://jsfiddle.net/8NynQ/

<div>
    <label for="radio1">
         <input type="radio" name="test" value="radio1" /> Radio 1
    </label>
    <input type="text" name="for_radio1[]" class="radio1" disabled="true" />
    <input type="text" name="for_radio1[]" class="radio1" disabled="true" />
    <label for="radio2">
       <input type="radio" name="test" value="radio2" /> Radio 2
    </label>
    <input type="text" name="for_radio2[]" class="radio2" disabled="true" />
    <input type="text" name="for_radio2[]" class="radio2" disabled="true" />
</div>

这是js

$(document).ready(function(){
$('input[type=radio][name=test]').click(function(){
    var related_class=$(this).val();
    $('.'+related_class).prop('disabled',false);

    $('input[type=radio][name=test]').not(':checked').each(function(){
        var other_class=$(this).val();
        $('.'+other_class).prop('disabled',true);
    });
});
});
于 2013-06-22T08:54:42.977 回答
2

例子:

HTML

<form id="myform" name="myform" action="" method="post">
    <input type="radio" name="group1" value="Milk" checked="checked" />Milk
    <input type="text" name="text1" value="milk1" />
    <input type="text" name="text2" value="milk2" />
    <br/>
    <input type="radio" name="group1" value="Cheese" />Cheese
    <input type="text" name="text3" value="cheese1" disabled="disabled" />
    <input type="text" name="text4" value="cheese2" disabled="disabled" />
</form>

Javascript

var form = document.forms['myform'];
form.group1[0].onfocus = function () {
    form.text1.disabled = form.text2.disabled = false;
    form.text3.disabled = form.text4.disabled = true;
}
form.group1[1].onfocus = function () {
    form.text1.disabled = form.text2.disabled = true;
    form.text3.disabled = form.text4.disabled = false;
}

jsfiddle

于 2013-06-22T08:52:13.947 回答
1

你是这个意思吗?

http://jsfiddle.net/UuwVG/

HTML:

<input type="radio" id="textbox1" onclick="check(this.id)" checked="true" />
<textarea id="textarea1"></textarea>
<br>
<input type="radio" id="textbox2" onclick="check(this.id)"/>
<textarea id="textarea2" disabled="true"></textarea>

Javascript:

function check(id) { 
var thistextbox = id.replace('box', 'area');
var othertextarea = 'textarea1';
if(id.slice(-1) == '1'){
    othertextarea = 'textarea2';
}
var othertextbox = othertextarea.replace('area', 'box');
document.getElementById(id).checked = true;
document.getElementById(thistextbox).disabled = false;
document.getElementById(othertextarea).disabled = true;
document.getElementById(othertextbox).checked = false;

}

于 2013-06-22T09:11:23.197 回答