javascript - 如何做动态div - 展开折叠循环

标签 javascript jquery html forms

我需要一个函数,允许我的客户在表单上添加更多 div 或不添加更多 div,例如单击按钮 (+),如循环

这个循环应该重复 div 和添加更多 div 的按钮,如下图所示

enter image description here

有人知道如何做到这一点吗?

展开折叠是最好的方法吗?

非常感谢!

最佳答案

我不确定您的愿望是什么,所以我发布了两个选项:

如果您想扩展隐藏的现有字段并假设您可以使用 JQUery:

<table>
    <tr>
        <th>Field1</th>
        <td><input type="text" value="" /></td>
    </tr>
    <tr>
        <th>Field2</th>
        <td><input type="text" value="" /></td>
    </tr>
    <tr class="hiddenarea" style="display: none;">
        <th>Field3 (Hidden)</th>
        <td><input type="text" value="" /></td>
    </tr>
</table>
<input id="show_hide" value="Collapse/Expand" />

<script type="text/javascript">
    $("#show_hide").click(function() {
        $(".hiddenarea").toggle();
    });
</script>

////////////////////////////////////

向表格添加新行的第二种解决方案是:

<table id="mytable">
    <tr>
        <th>Field1</th>
        <td><input type="text" value="" /></td>
    </tr>
    <tr>
        <th>Field2</th>
        <td><input type="text" value="" /></td>
    </tr>
</table>
<input id="show_hide" value="Collapse/Expand" />

<script type="text/javascript">
    $("#show_hide").click(function() {
        $("#mytable").append('<tr><td>New field</td><td><input type="text" value="" /></td></tr>');
    });
</script>

关于javascript - 如何做动态div - 展开折叠循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995858/

相关文章:

javascript - 使用 Javascript 循环创建 html 内容

javascript - 将子href的点击功能绑定(bind)到父div

php - 刷新 jquery ajax 响应中的特定元素

html - 文本框格式有问题?

javascript - 如何循环播放来自同一 src 的动画和淡入淡出图像?

javascript - 像 JavaScript 函数一样执行 facebook 吗?

jquery - 在 JQUERY 代码中使用两个 CSS 属性

dom插入后执行javascript

javascript - Ajax 表单提交设置 enctype

jquery - highcharts - 更新系列以使其类别比以前少时出错