1

我正在尝试为个人和公司做一个单独的公式

如果访问者选中“个人”单选按钮,它将显示一个表格

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

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

<fieldset>Ps:因为我的其他javascript,除了现有的我不能使用

4

1 回答 1

2

您需要在字段周围包裹一些东西以显示/隐藏,即使只是<div>带有 ID 的标签。

然后对于您的单选按钮,您需要添加onchange&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>
于 2013-02-19T23:31:32.930 回答