Javascript jQuery添加链接以动态删除控件

Javascript jQuery添加链接以动态删除控件,javascript,jquery,html,Javascript,Jquery,Html,我可以生成控件,但无法删除它们。我正在尝试添加一个“链接”,它将调用一个函数来删除动态创建的控件。控件和链接彼此相邻。下面是创建控件的Java脚本和标记: <script type="text/javascript"> $(function() { // when document has loaded // <input id="File1" type="file" runat="server" size="60" />

我可以生成控件,但无法删除它们。我正在尝试添加一个“链接”,它将调用一个函数来删除动态创建的控件。控件和链接彼此相邻。下面是创建控件的Java脚本和标记:

<script type="text/javascript">
        $(function() { // when document has loaded

            //   <input id="File1" type="file" runat="server" size="60" />

            var i = $('input').size() + 1; // check how many input exists on the document and add 1 for the add command to work


            $('a#add').click(function() { // when you click the add link
                $('<p><input type="file" id="' + i + '" name="' + 'dynamic:' + i + '" /> <a href="#" id="' + 're' + i + ' " onclick="removeControl("' + '\'#' + i + '\''+ '")">+</a></p>').appendTo('body'); // append (add) a new input to the document.

                // if you have the input inside a form, change body to form in the appendTo
                i++; //after the click i will be i = 3 if you click again i will be i = 4s
            });

            function removeControl(controlId) {

                $(controlId).remove();
            }


        }); </script>


   <div>
        <a href="#" id="add">Add</a>

<br />


<p><input type="file" id="1" /></p>

    </div>

$(函数(){//当文档已加载时)
//   
var i=$('input').size()+1;//检查文档上存在多少输入,并添加1以使add命令正常工作
$('a#add')。单击添加链接时,单击(function(){//
$(“

”).appendTo('body');//向文档追加(添加)新输入。 //如果在表单中有输入,请在appendTo中将body更改为form i++;//单击后,如果再次单击,我将为i=3,我将为i=4s }); 函数removeControl(controlId){ $(controlId).remove(); } });

用于创建链接以删除控件的脚本不工作。当我使用Firebug查看源代码时,onClick属性没有正确标记

<a href="#" id="' + 're' + i + ' " onclick="removeControl("' + '\'#' + i + '\''+ '")">+</a>

我只想添加一个链接来删除生成的控件和链接本身


谢谢。

尝试将removeControl功能更改为:

function removeControl(driver) {
  $(driver).closest("p").remove();
}
然后,将对锚点中函数的调用更改为:

onclick="removeControl(this);return false;"

如果您想从Dom中删除通过javascript包含的元素,那么应该很方便…


工作解决方案:

$(函数(){
//初始化计数器
var计数器=0;
//添加元素
$('#添加')。单击(函数(){
计数器++//递增计数器
var add_input='';
var add_link='';
//如果计数器小于4,则追加元素

如果(计数器)总是将dom插入减少到最小值。

是否要删除您添加的
或整个
?还有一件事,IDs不能以数字开头,您需要在它前面加上前缀,例如
“file1”
,等等。我要删除和添加的控件。是的,删除将同时删除这两个控件。如何仅允许添加4个控件?请参见上面的编辑。如果需要添加4个以上的控件,只需根据需要更改If语句中的值即可。
$('a.remove').live('click', function() {
  // Stuff to remove
});
<script type="text/javascript">
    $(function() {
       
        // Init counter
        var counter = 0;
        
        // Add Elements
        $('#add').click(function() {

            counter++ // Increment counter

            var add_input = '<p><input type="file" />';
            var add_link = '<a href="#" class="remove">Remove</a>';

            // Append Elements if counter smaller 4
            if (counter <= 4) {
                $('body').append('<p>' + add_input + add_link + '</p>');
            }
            return counter;
         });

         // Remove Elements
         $('.remove').live('click', function() {
             counter--; // Decrement counter
             $(this).parent('p').remove();
         });
     });
</script>

<a href="#" id="add">Add</a>