单选按钮更改可见输入字段

时间:2013-02-19 23:20:11

标签: forms hide show radio

我正在尝试为个人和公司单独制定一份处方

如果访问者选中“个人”单选按钮,则会显示一个表单

如果访问者检查“公司”单选按钮,它将显示不同的表单

 <fieldset>
                    <input type="radio" name="tipo_cadastro" value="individual_form" id="individual_form" style="margin:0px !important">
                    <strong>Individual Form</strong><br/>

                    <input type="radio" name="tipo_cadastro" value="corporation_form" id="corporation_form" style="margin:0px !important">
                    <strong>Corporation Form</strong>

            <!-- If Individual Form is checked -->
            <legend>Personal Data</legend>
            <label for="Name">Full Name</label>
            <input id="Name" type="text" />
            <label for="City">City</label>
            <input id="City" type="text" />
            <label for="Email">Email</label>
            <input id="Email" type="text" />

            <!-- If Corporation Form is checked -->
            <legend>Corporation Data</legend>
            <label for="Name">Name</label>
            <input id="Name" type="text" />
            <label for="City">City</label>
            <input id="City" type="text" />
            <label for="Email">Email</label>
            <input id="Email" type="text" />

        </fieldset>

Ps:由于我的其他javascript,我不能使用其他<fieldset>而不是现有的

1 个答案:

答案 0 :(得分:2)

你需要在字段周围包装一些内容来显示/隐藏,甚至只需要一个带有ID的<div>标签。

然后,对于您的单选按钮,您需要添加onchange&amp; onmouseup个事件处理程序(用于说明人们点击更改值或使用键盘)。 例如

<script type="text/javascript">
function onchange_handler(obj, id) {
    var other_id = (id == 'personal')? 'corporate' : 'personal';
    if(obj.checked) {
        document.getElementById(id + '_form_fields').style.display = 'block';
        document.getElementById(other_id + '_form_fields').style.display = 'none';
    } else {
        document.getElementById(id + '_form_fields').style.display = 'none';
        document.getElementById(other_id + '_form_fields').style.display = 'block';
    }
}
</script>
<fieldset>
    <input type="radio" name="tipo_cadastro" value="individual_form" id="individual_form" style="margin:0px !important" onchange="onchange_handler(this, 'personal');" onmouseup="onchange_handler(this, 'personal');">
    <strong>Individual Form</strong><br/>

    <input type="radio" name="tipo_cadastro" value="corporation_form" id="corporation_form" style="margin:0px !important" onchange="onchange_handler(this, 'corporate');" onmouseup="onchange_handler(this, 'corporate');">
    <strong>Corporation Form</strong>

    <!-- If Individual Form is checked -->
    <div id="personal_form_fields">
        <legend>Personal Data</legend>
        <label for="Name">Full Name</label>
        <input id="Name" type="text" />
        <label for="City">City</label>
        <input id="City" type="text" />
        <label for="Email">Email</label>
        <input id="Email" type="text" />
    </div>

    <!-- If Corporation Form is checked -->
    <div id="corporate_form_fields" style="display: none;">
        <legend>Corporation Data</legend>
        <label for="Name">Name</label>
        <input id="Name" type="text" />
        <label for="City">City</label>
        <input id="City" type="text" />
        <label for="Email">Email</label>
        <input id="Email" type="text" />
    </div>
</fieldset>