-1

当用户单击 addAgent 按钮时,我需要添加 4 个输入字段。我还允许用户从其中一个中删除。如果用户添加 4 个输入字段,我想禁用添加按钮。如果用户删除了这 4 个输入字段之一,则启用添加按钮以添加一个或两个添加,具体取决于用户删除或添加的输入数量。但最多只有四个。我想用 jQuery 来做这件事,我能得到任何帮助或样本吗?谢谢!

4

2 回答 2

2

这是工作的jsfiddle [DEMO]

 <div id="add">Add</div>
    <div id="container"></div>

$('#add').bind('click', function() {
    if($('#container').find('input').length < 4) {
        $('#container').append('<div><input type="text"><span class="remove">remove</span></div>');
    }
})

$('body').on('click', '.remove', function() {
    if($('#container').find('input').length > 0) {
        $(this).parent().remove();
    }
})
于 2013-08-07T17:07:09.747 回答
-1

也许这就是你要找的?您可以像这样创建四个输入:

 <div class='control'>
        <input type='text' value='' />
        <a href='#' class='remove'>Remove</a>
    </div>
    <div class='control'>
        <input type='text' value='' />
        <a href='#' class='remove'>Remove</a>
    </div>
    <div class='control'>
        <input type='text' value='' />
        <a href='#' class='remove'>Remove</a>
    </div><div class='control'>
        <input type='text' value='' />
        <a href='#' class='remove'>Remove</a>
    </div>

然后你可以编写 jquery 来隐藏它们并在单击按钮时显示它们。

像这样:http: //jsfiddle.net/kSH7M/1

于 2013-08-07T16:54:32.910 回答