修复表的大小,即使CSS中“display none or block”也是如此

时间:2013-05-15 17:56:25

标签: html css

我有这个HTML代码:

<form:form method="get" action="${pageContext.request.contextPath}/get_info">
    <table id="tabmenu">
        <tr>
            <td><input type="radio" name="choice" id="np" onClick="Affichenp();"/>Nomet Prenom</td>
            <td><input type="radio" name="choice"id="ns" onClick="Afficheppr();"/>Numérode somme</td>
            <td><input type="radio" name="choice" id="cn" onClick="Affichecin();"/>CIN</td>
        </tr>
        <tr>
            <td><input type="text" name="nom" id="nom" class="round default-width-input" /></td>
            <td><input type="text" name="ppr" id="ppr" class="round default-width-input" /></td>
            <td><input type="text" name="cin" id="cin" class="round default-width-input" /></td>
            <td><input class="button round blue image-right ic-right-arrow" type="submit" value=""></td>
        </tr>
    </table>
</form:form>

这是javascript调用的函数代码:

<script type="text/javascript">

function Affichenp() {
    document.getElementById("nom").style.display = "block";
    document.getElementById("ppr").style.display = "none";
    document.getElementById("cin").style.display = "none";
}

function Afficheppr() {
    document.getElementById("nom").style.display = "none";
    document.getElementById("ppr").style.display = "block";
    document.getElementById("cin").style.display = "none";
}

function Affichecin() {
    document.getElementById("nom").style.display = "none";
    document.getElementById("ppr").style.display = "none";
    document.getElementById("cin").style.display = "block";
}

当我点击一个收音机盒时,我希望它下面的输入显示,所以当我点击单选按钮时,表格的行数会发生变化。换句话说,当我第一次打开我的页面时,我没有输入,所以我选择了一个单选按钮给我一个输入来填充它...所以td的宽度发生了变化。任何解决方案?

2 个答案:

答案 0 :(得分:3)

尝试使用style.visibility = "hidden"style.visibility = "visible"代替!

答案 1 :(得分:2)

您写的问题是,<td>的宽度会在添加<input>后发生变化。我的建议是使用大于<td>宽度的CSS为<input>提供默认宽度(200px只是一个示例,您可以根据需要进行更改):

#tabmenu td {width:200px;}

此外,您可以通过将id作为参数来简化您的功能:

function Affiche(id) {
    document.getElementById("nom").style.display = "none";
    document.getElementById("ppr").style.display = "none";
    document.getElementById("cin").style.display = "none";
    document.getElementById(id).style.display = "block";
}