简体   繁体   English

动态添加/删除输入行 javascript,jquery

[英]Dynamic add/delete input rows javascript,jquery

The dynamic add/delete input rows is not working properly.动态添加/删除输入行工作不正常。 The rows are created using add function, but it is not deleted properly.这些行是使用 add 函数创建的,但没有正确删除。 Basically the delete function call is not working.基本上删除函数调用不起作用。

$(document).ready(function(){
    var counter = 1; //initlal text box count
    $("#addButton").click(function () {
        if(counter > 3){
            alert("Only 3 textboxes allowed");
            return false;
        }
        var selectfield = $('#selectcolumnlist option:selected').val();
        var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv');
        newTextBoxDiv.after().html('<label>'+ selectfield + ' : </label>' + '<input type="text" name="textbox_' + selectfield + '" id="textbox_'+selectfield+'" placeholder="' + selectfield + '" value="" /><input type="button" value="Remove Button" class="remove_this" id="removeid" />');
        newTextBoxDiv.appendTo("#TextBoxesGroup");
        counter++;
        alert(counter);
    });

    $("#removeid").click(function() {
        alert("i'm in");
    });
}

Assuming you're creating elements in a correct way, I mean, using unique ids, you should use event-delegation :假设您以正确的方式创建元素,我的意思是,使用唯一 ID,您应该使用event-delegation

// This example selects the element with the class 
// .removeid (here you need to set a specific class) or set something unique
// per new dynamically created element.
$("#TextBoxesGroup").on('click', '.remove_this', (function() {
    alert("i'm in");
}));

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

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