简体   繁体   English

HTML&JS-如何显示包含一个以上文本字段的DIV

[英]HTML & JS - How to display a DIV which contains one text field more than once

I am new to development. 我是开发新手。 Right now I have a requirement in a form as I need to display a field with Add and Remove buttons. 现在我对表单有要求,因为我需要显示带有“添加”和“删除”按钮的字段。 If user clicks on Add button, app should display one more field. 如果用户单击“添加”按钮,则应用程序应再显示一个字段。 If user clicks on Remove button, app should hide the respective field. 如果用户单击“删除”按钮,则应用程序应隐藏相应的字段。

Please suggest me how can I make a Text field to display more than once. 请建议我如何使“文本”字段显示不止一次。 Moreover I need get the text field data in PHP hence I should have different name for fields. 此外,我需要在PHP中获取文本字段数据,因此我应该对字段使用不同的名称。

Thank you in advance! 先感谢您!

Raja 拉贾

It must be helpful for you. 它必须对您有帮助。

html: 的HTML:

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
<p>
    <label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
</p>
</div>

Css: CSS:

* { font-family:Arial; }
h2 { padding:0 0 5px 5px; }
h2 a { color: #224f99; }
a { color:#999; text-decoration: none; }
a:hover { color:#802727; }
p { padding:0 0 5px 0; }

input { padding:5px; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -web-kit-border-radius:4px; -khtml-border-radius:4px; }

javascript: javascript:

$(function() {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function() {
            $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
            i++;
            return false;
    });

    $('#remScnt').live('click', function() { 
            if( i > 2 ) {
                    $(this).parents('p').remove();
                    i--;
            }
            return false;
    });
 });

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM