开发者

delete specific entries

I'm creating a jquery script that could create new form fields, i can create them but how can i delete the specific entries (referenced by class name class="inimene_'+count+'")

<script>
var count = 0;
function add(){
count ++;
var code = '<div class="inimene_'+count+'"><p>Eesnimi: <input type="text" id="eesnimi_'+count+'" /></br>Perenimi: <input type="text" id="perenimi_'+count+'" /></br>Pilet: <select><option selected>Vali pileti liik</option><option value="100.00 Kr">Täiskasvanu</option><option value="25.00 Kr">Õpilane 5-12kl</option><option value="3">Laps kuni 4kl<开发者_开发百科;/option></select>Summa: <input type="text" id="summa_'+count+'" /> Linn/Maakond <input type="text" id="linn_'+count+'" /> Rivi: <input type="checkbox" id="rivi_'+count+'" /><input type="button" id="kustuta" onclick="delete()" value="Kustuta" /></p></div>'
$('#container').append(code)
$('#val').val(count)
}
function delete(){
$('.inimene:last').remove();
}
 $(document).ready(function(){
 $('.add').click(add)
});
</script>


I think you should ID them one way and class them another and delete based on class.

So,

var code = '<div class="inimene" ID="inimene_'+count+'"><p>Eesnimi: <input type="text" id="eesnimi_'+count+'" /></br>Perenimi: <input type="text" id="perenimi_'+count+'" /></br>Pilet: <select><option selected>Vali pileti liik</option><option value="100.00 Kr">Täiskasvanu</option><option value="25.00 Kr">Õpilane 5-12kl</option><option value="3">Laps kuni 4kl</option></select>Summa: <input type="text" id="summa_'+count+'" /> Linn/Maakond <input type="text" id="linn_'+count+'" /> Rivi: <input type="checkbox" id="rivi_'+count+'" /><input type="button" id="kustuta" onclick="delete()" value="Kustuta" /></p></div>'
$('#container').append(code)
$('#val').val(count)
}
function delete(){
$('.inimene:last').remove();
}
 $(document).ready(function(){
 $('.add').click(add)
});


To select by the inimene class (appending a count variable), you could do this:

$('#container > .inimene_' + count).remove();

So your delete function may look like this:

function delete_it( count ){
    $('#container > .inimene_' + count).remove();
}

delete_it( 2 );

Or if you didn't bother appending the count to the class name, you could just select by class inimene and narrow it down by its index:

function delete_it( count ){
    $('#container > .inimene:eq(' + count + ')').remove();
    // $('#container > .inimene').eq( count ).remove();  // or this
}

delete_it( 2 );


How do you specify which div you want to remove? If you simply want to remove the last added element you should add another class that is the same for every div and then select all elements with that class with jQuery, and remove the last element.

If for example you added another class to the created divs called "inimene" then your delete code would look like this:

$('.inimene').last().remove();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜