1

我正在尝试根据选中的单选按钮显示/隐藏文本字段。这是我的代码;如果我不使用表格标签,它工作正常,使用表格标签时,Javascript 不起作用

<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>
<table>
     <tr>
     <td colspan="2">
    <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>

    <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>
    </td><tr>

    <!-- If Individual Form is checked -->
    <div id="personal_form_fields">
             <tr><td>First Name</td>
                 <td><input type="text" name="First_Name" value=""></td>
             </tr>
             <tr><td>Last Name</td>
                 <td><input type="text" name="Last_Name" value=""></td>
             </tr>
    </div>

    <!-- If Corporation Form is checked -->
    <div id="corporate_form_fields" style="display: none;">
      <tr><td>Company</td>
         <td><input type="text" name="company_name" value=""></td>
      </tr>
    </div>
</table>
4

2 回答 2

0

只需向每个组的 TR 添加一个类并显示/隐藏该类...

<script type="text/javascript">

function onchange_handler(obj, id) {
    var other_id = (id == 'personal')? 'corporate' : 'personal';
    if(obj.checked) 
    {
        class_display(id + '_form_fields','block');
        class_display(other_id + '_form_fields','none');
    } else {
        class_display(id + '_form_fields','none');
        class_display(other_id + '_form_fields','block');
    }
}

function class_display(tr_class,display)
{
    var tr_ele = document.getElementsByClassName(tr_class);
    for (var i = 0; i < tr_ele.length; ++i) {
        var item = tr_ele[i];  
        item.style.display = display;
    }
}
</script>
<table>
    <tr>
        <td colspan="2">
            <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');" checked>
            <strong>Individual Form</strong>

            <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>
        </td>
    <tr>

    <!-- If Individual Form is checked -->
    <tr class="personal_form_fields">
        <td>First Name</td>
        <td><input type="text" name="First_Name" value=""></td>
    </tr>
    <tr class="personal_form_fields">
        <td>Last Name</td>
        <td><input type="text" name="Last_Name" value=""></td>
    </tr>

    <!-- If Corporation Form is checked -->
    <tr class="corporate_form_fields" style="display: none;">
        <td>Company</td>
        <td><input type="text" name="company_name" value=""></td>
    </tr>

</table>
于 2013-08-12T19:11:14.730 回答
0

putvande 所说的“奇怪标记”可能意味着您<div id="personal_form_fields">在表格中,其父项是table标签。那是不对的。tr应该包含,td包含div, 而不是相反。

如果您尝试更改可见性,则此语法错误可能是问题所在。

于 2013-08-12T18:06:08.443 回答