2013-07-15 34 views
40

Tôi muốn có thể thêm nhiều hàng vào một div và cũng có thể xóa chúng. Tôi có nút '+' ở đầu trang để thêm nội dung. Sau đó, ở bên phải của mỗi hàng có một nút '-' đó là để loại bỏ hàng đó. Tôi chỉ không thể tìm ra mã javascript trong ví dụ này.Thêm/xóa HTML bên trong div bằng cách sử dụng JavaScript

Đây là cấu trúc HTML cơ bản của tôi:

<input type="button" value="+" onclick="addRow()"> 

<div id="content"> 

</div> 

Đây là những gì tôi muốn thêm vào bên trong div nội dung:

<input type="text" name="name" value="" /> 
<input type="text" name="value" value="" /> 
<label><input type="checkbox" name="check" value="1" />Checked?</label> 
<input type="button" value="-" onclick="removeRow()"> 
+1

Bạn có thể đăng mã JavaScript của mình cho đến nay không? – elclanrs

+0

@elclanrs Tôi đã thử theo hướng dẫn này: http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/ nhưng không nhận được mã để làm việc với ví dụ của tôi. Tôi cũng đã xem xét câu trả lời cập nhật mà anh ấy đã đăng nhưng tôi thực sự không hiểu mã vì tôi khá mới với JavaScript. –

Trả lời

74

Bạn có thể làm điều gì đó như thế này.

function addRow() { 
    var div = document.createElement('div'); 

    div.className = 'row'; 

    div.innerHTML = 
     '<input type="text" name="name" value="" />\ 
     <input type="text" name="value" value="" />\ 
     <label> <input type="checkbox" name="check" value="1" /> Checked? </label>\ 
     <input type="button" value="-" onclick="removeRow(this)">'; 

    document.getElementById('content').appendChild(div); 
} 

function removeRow(input) { 
    document.getElementById('content').removeChild(input.parentNode); 
} 
+0

Tôi muốn có thể thêm nhiều hàng. Câu hỏi của tôi hơi không rõ ràng nên tôi đã chỉnh sửa nó. –

+0

Điều này hỗ trợ nhiều hàng. –

+1

Ồ, đúng vậy. Chỉ cần thực hiện một lỗi khi thử mã của bạn. Nó đang làm việc bây giờ. Chúc mừng! –

-1

làm cho một lớp học cho nút cho phép nói:

`<input type="button" value="+" class="b1" onclick="addRow()">` 

js của bạn sẽ trông giống như sau:

$(document).ready(function(){ 
    $('.b1').click(function(){ 
     $('div').append('<input type="text"..etc '); 
    }); 
}); 
+2

Anh ấy không đề cập đến jQuery. –

2

Bạn có thể sử dụng chức năng này để thêm trẻ vào phần tử DOM.

function addElement(parentId, elementTag, elementId, html) 

{ 


// Adds an element to the document 


    var p = document.getElementById(parentId); 
    var newElement = document.createElement(elementTag); 
    newElement.setAttribute('id', elementId); 
    newElement.innerHTML = html; 
    p.appendChild(newElement); 
} 



function removeElement(elementId) 

{ 

    // Removes an element from the document 
    var element = document.getElementById(elementId); 
    element.parentNode.removeChild(element); 
} 
1

hãy thử sau đây để tạo ra

function addRow() 
    { 
     var e1 = document.createElement("input"); 
     e1.type = "text"; 
     e1.name = "name1"; 

     var cont = document.getElementById("content") 
     cont.appendChild(e1); 

    } 
3

Để loại bỏ nút bạn có thể thử giải pháp này nó đã giúp tôi.

var rslt = (nodee=document.getElementById(id)).parentNode.removeChild(nodee); 
Các vấn đề liên quan