Remove()函数在javascript中不起作用

时间:2014-05-14 08:51:32

标签: javascript jquery function typeerror

这里我试图在我的html中动态添加和删除文本框。文本框已成功加入,但我的删除功能无法正常工作

请告诉我为什么这个功能不起作用

  var counter = 2;
    $(document).ready(function () {

    $("#addButton").click(function () {
        if (counter > 5) {
            alert("Limit Exceeds");
            return false;
        }
        var $wrap = $('#TextBoxesGroup');
        var dynamichtml = '<div class="mcity"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/>  </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 1 + '" class="auto"/> </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 11 + '" class="auto"/> </div>';
        $wrap.append(dynamichtml);
        counter++;
    });

    $("#removeButton").click(function () {
        if (counter == 1) {
            alert("No more textbox to remove");
            return false;
        }

        counter--;
        $("#TextBoxesGroup").find("#divleft_" + counter).remove();
        $("#TextBoxesGroup").find("#divright_" + counter).remove();


    });

4 个答案:

答案 0 :(得分:0)

我想你想实现这个目标

var counter = 1;


$("#addButton").click(function () {
    if (counter > 5) {
        alert("Limit Exceeds");
        return false;
    }
    var $wrap = $('#TextBoxesGroup');
    var dynamichtml = '<div class="box"><div class="mcity"><label> Leaving from</label><input type="text" name="textbox' + counter + '" id="textbox' + counter + '" class="auto"/>  </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 1 + '" class="auto"/> </div><div class="mcity"> <label> Going to</label>  <input type="text" name="textbox' + counter + '" id="textbox' + counter + 11 + '" class="auto"/> </div></div>';
    $wrap.append(dynamichtml);
    counter++;
});

$("#removeButton").click(function () {
    if (counter == 1) {
        alert("No more textbox to remove");
        return false;
    }

    counter--;
    console.log(counter)
    $("#TextBoxesGroup div.box:last-child").remove();



});

请检查此fiddle

答案 1 :(得分:0)

这是我的JSFiddle链接

var counter = 1;
$(document).ready(function () {

$("#addButton").click(function () {
    if (counter > 5) {
        alert("Limit Exceeds");
        return false;
    }
    var $wrap = $('#TextBoxesGroup');
    var dynamichtml = "<div id='divleft_"+(counter)+"' ><div class='mcity' ><label> Leaving from</label><input type='text' name='textbox" + counter + "' id='textbox" + counter + "' class='auto'/>  </div><div class='mcity'> <label> Going to</label>  <input type='text' name='textbox" + counter + "' id='textbox"+ counter + 1 + "' class='auto'/> </div><div class='mcity'> <label> Going to</label>  <input type='text' name='textbox" + counter + "' id='textbox" + counter + 11 + "' class='auto'/> </div></div>";
    $wrap.append(dynamichtml);
    counter++;
});

$("#removeButton").click(function () {
    if (counter == 1) {
        alert("No more textbox to remove");
        return false;
    }
    counter--;
    $("#TextBoxesGroup").find("#divleft_" + counter).remove();
});
});

答案 2 :(得分:0)

您正在搜索似乎不存在的ID。

$("#TextBoxesGroup").find("#divleft_" + counter).remove();
$("#TextBoxesGroup").find("#divright_" + counter).remove();

我想你可能只是想将你的dynamichtml包装在这样的<div>标签中:

<div id='divleft_"+counter+"' >

答案 3 :(得分:-1)

从上面的问题我理解并开发了这个,检查一次

[http://jsfiddle.net/UDq9W/][1]

好的@Azad chouhan