簡體   English   中英

根據選中的單選按鈕顯示/隱藏div

[英]show/hide div based on radio button checked

我試圖根據選中的單選按鈕顯示/隱藏文本字段。 這是我的代碼; 如果我不使用表格標簽,效果很好,使用表格標簽時,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>

putvande的“奇怪標記”可能意味着您的<div id="personal_form_fields">在表中,其父級是table標簽。 那是不對的。 tr應該包含td ,其中包含div ,而不是相反。

如果您嘗試更改可見性,則可能是此語法錯誤。

只需將一個類添加到每個組的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM