【问题标题】:Remove() function is not working in javascriptRemove() 函数在 JavaScript 中不起作用
【发布时间】:2014-07-02 05:25:09
【问题描述】:

在这里,我尝试在我的 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();


    });

【问题讨论】:

  • 请发布您的 HTML,因为我在任何地方都找不到 divLeft_

标签: javascript jquery function typeerror


【解决方案1】:

我想你想实现这个

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

【讨论】:

    【解决方案2】:

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

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

    好的@Azad chouhan

    【讨论】:

    【解决方案3】:

    这是我的 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();
    });
    });
    

    【讨论】:

    • 如果有任何问题,您可以参考此链接jsfiddle.net/UDq9W 获取 html 和剩余内容。
    【解决方案4】:

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

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

    我认为您可能只是想将您的动态html 包装在一个 &lt;div&gt; 标记中,如下所示:

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

    【讨论】:

    • 对不起,我在回答的时候被叫走了,我回来提交的时候好像有点晚了。但很高兴你得到了答案。
    • hahahaha dnt wry bro 但我也很感谢你的回答谢谢你的回复:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2012-01-25
    • 1970-01-01
    • 2011-08-31
    • 2013-09-27
    • 2011-03-20
    相关资源
    最近更新 更多