简体   繁体   English

单击按钮时如何删除2个文本框

[英]How do i delete 2 textboxes when i click a button

I have a form whereby users can click on the add button to produce 2 more textboxes at every click. 我有一个表单,用户可以单击“添加”按钮来在每次单击时产生两个以上的文本框。 It works perfectly fine until i added in a function name deleterow(). 直到我在函数名称中添加deleterow()为止,它的工作原理都很好。 Once i added that function, my addrow() does not work anymore. 添加该函数后,我的addrow()不再起作用。 This are my codes for addrow(). 这是我的addrow()代码。

<button onclick ="addRow()" value="Add Row">Add Row</button>
<button onclick ="deleterow()" value ="Reset">Reset</button>        
<script>
    var x=1
    var count=0;

    function addRow()
    {

        if(count <= 10)
        {
            var d = document.getElementById('div');
            d.innerHTML += "<input type='text' id='txta"+ x +"'><span class =wordtab></span>";
            d.innerHTML += "<input type='text' id='txtb"+ x +"'><br><br>";
            count++;
            x++
        }
        else
        {
            alert("Maximum 10 Skills");
        }
    }

These are my codes for deleterow(). 这些是我的deleterow()代码。 I am also not sure if my codes are correct for deleting 2 'latest' textboxes everytime i click on the delete button. 我也不确定每次单击删除按钮时,我的代码是否正确,无法删除2个“最新”文本框。

    function deleterow()
    {
        for(int i=1;i<=10;i++)
        {
        var element1 = document.getElementById('txta'+i); // will return element
        element1.parentNode.removeChild(element1); // will remove the element from DOM  
        var element2 = document.getElementById('txtb'+i); // will return element
        element2.parentNode.removeChild(element2); // will remove the element from DOM
        }

    }
</script>
<div id="div">

Oh and lastly, how can i do so that everytime i add a new row, my buttons will automatically be below all of the text boxes. 哦,最后,我该怎么做,以便每次我添加新行时,我的按钮都会自动位于所有文本框的下方。 Currently, it will be stuck at the top of the page. 当前,它将停留在页面顶部。 Thanks!:) 谢谢!:)

This is my css 这是我的CSS

.wordtab 
        {
            min-width: 85px; 
            display: inline-block;
        }

This is my current output if i were to insert 11 rows and delete row number 5 and 6. 如果我要插入11行并删除行号5和6,这是我当前的输出。

在此处输入图片说明

在此处输入图片说明

This is my output when i delete all 11 rows and add 2 more rows 这是我删除所有11行并再添加2行时的输出

Because you're using jquery you could use click events instead of inline onclick 因为您使用的是jquery,所以可以使用click事件代替内联onclick

I think you should add reset button to every line so you could remove it, for the the add button it will be stuck at the top of the page. 我认为您应该向每行添加重置按钮,以便可以将其删除,因为添加按钮将被卡在页面顶部。 you have just to place it after the div. 您只需将其放在div之后。

Check the example bellow, this helps. 查看下面的示例,这很有帮助。


 var x=1; var count=0; //click Event for the add button $('body').on('click','#add',function(){ if(count <= 10){ //add the two inputs + the reset button to a div with class 'line' then append //this div to #div $('#div').append("<div class='line'><input type='text' id='txta"+ x +"'><span class='wordtab'></span><input type='text' id='txtb"+ x +"'><button class='delete' value='Reset'>Reset</button></div>"); count++; x++ } else alert("Maximum 10 Skills"); }); //click Event for the delete button $('body').on('click','.delete',function(){ //when the user click on delete get the parent div with class 'line' of clickable //button and remove it $(this).closest('.line').remove(); count--; }); 
 .wordtab { min-width: 85px; display: inline-block; } .line{ margin-bottom: 15px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="div"></div> <button id='add' value="Add Row">Add Row</button> 

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

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