显示基于复选框隐藏表行

时间:2013-11-20 11:03:57

标签: javascript jquery html html5

在这个Fiddle中我试图显示/隐藏包含基于复选框选择的文件输入的表格行。但showHide函数没有被调用。

<div align="center" class="divBody">
<br />
<div id="controlHost">
    <div id="outerPanel">
        <table width="100%" cellpadding="2" cellspacing="5">
            <tr align="left">
                <td colspan="2">
                    <input type="checkbox" id="c1" onclick="showHide()">only Textbox</input>
                </td>
            </tr>
            <tr align="left" id="fileLabel">
                <td colspan="2">
                    <span class="message" >Select file</span>
                </td>
            </tr>
            <tr align="left" id="fileBox">
                <td valign="top" style="height:100%; width:70%;">
                    <input type="file" id="FileInput" multiple="false" class="fileInput" style="height:100%; width:100%;"/>
                </td>
            </tr>
         <tr align="left">
                <td colspan="2">
                    <span class="message" >Types</span>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" id="txtTypes" tabindex="0" style="margin-left:1px;width:100%" maxlength="50" >
                </td>
            </tr>
            <tr>
                <td align="center">
                    <input type="button" id="upload" name="Upload" value="Update" onclick="startUpload('FileInput', 1048576, 'uploadProgress', 'statusMessage', 'upload', 'cancel');"
                        class="button" />
                    <input type="button" id="cancel" name="Cancel" value="Cancel" disabled="disabled"
                        onclick="cancelUpload();" class="button" />
                </td>
            </tr>
        </table>
    </div>
</div>

4 个答案:

答案 0 :(得分:3)

从代码本身可以清楚地看到你缺少jQuery库(在小提琴中,我看不到包含的库)。

document.getElementById('fileLabel').show();
在jQuery中,您可以将其简化为

$('#fileLabel').show();

.show()/.hide() 是jQuery方法。

$(document).ready(function () {
    $('#c1').on('change', function(){
        if ($(this).prop('checked')) {
            $('#filelabel').show();
            $('#fileBox').show();
        }
        else {
            $('#filelabel').hide();
            $('#fileBox').hide();
        }
    });
});

答案 1 :(得分:2)

小提琴中存在多个问题

  1. 在左侧面板的第二个下拉列表中选择No Wrap head / body - 当选择onload时,您的脚本将添加到window.onload = function(){//your code}包装器中,使该函数成为包装器函数的本地

  2. 您需要在页面中包含jQuery库

  3. show()/hide()这样的方法绑定到jQuery包装器对象

    仅文本框

  4. 然后

    jQuery(function () {
        $('#c1').change(function () {
            $('#fileLabel, #fileBox').toggle(this.checked)
        }).change()
    })
    

    演示:Fiddle

答案 2 :(得分:0)

功能是工作请在html的head部分写javascript

<head>
<script>
function showHide() {
    alert('called');
    var chbox = document.getElementById("c1");
    var vis = "none";
    for(var i=0;i<chboxs.length;i++) { 
        if(chbox.checked){
            alert('checked');
            document.getElementById('fileLabel').show();
            document.getElementById('fileBox').show();
            break;
        }
        else
        {
            alert('unchecked');
            document.getElementById('fileLabel').hide();
            document.getElementById('fileBox').hide();
            break;
        }
    }
}
</script>
</head>

答案 3 :(得分:0)

在左侧面板框架和扩展程序&gt;&gt;第二个下拉集没有换行

它将开始工作。