admin管理员组文章数量:1313006
I have been working all day on one of my first javascripts which is designed to have a user insert items into a text box using a field and submit button. Items that are put in the list have a remove button next to them.
I was wondering if anyone had any smart ideas about how I could remove items using the remove link next to the list. When this link is clicked I want to have it removed from the list but I can't really see a clean and simple way of doing it.
Demo Jsfiddle: /
// If JS enabled, disable main input
$("#responsibilities").prop('disabled', true);
// $("#responsibilities").addClass("hidden");
// If JS enabled then add fields
$("#resp").append('<input placeholder="Add responsibility" id="resp_input" ></input><input type="button" value="Add" id="add"> ');
// Add items to input field
var eachline='';
$("#add").click(function(){
var lines = $('#resp_input').val().split('\n');
var lines2 = $('#responsibilities').val().split('\n');
if(lines2.length>10)return false;
for(var i = 0;i < lines.length;i++){
if(lines[i]!='' && i+lines2.length<11){
eachline += lines[i] + '\n';
}
}
$('#responsibilities').text($("<div>" + eachline + "</div>").text()).before("<li>"+$("<p>"+lines+"</p>").text()+"<span class='right'><a href='#'>Remove</a></span></li>");
$('#resp_input').val('');
});
I know people have much more experience, so if anyone is willing to give me any help or advice on how this could be achieved I would really apprecaite it.
I have been working all day on one of my first javascripts which is designed to have a user insert items into a text box using a field and submit button. Items that are put in the list have a remove button next to them.
I was wondering if anyone had any smart ideas about how I could remove items using the remove link next to the list. When this link is clicked I want to have it removed from the list but I can't really see a clean and simple way of doing it.
Demo Jsfiddle: http://jsfiddle/spadez/ZTuDJ/46/
// If JS enabled, disable main input
$("#responsibilities").prop('disabled', true);
// $("#responsibilities").addClass("hidden");
// If JS enabled then add fields
$("#resp").append('<input placeholder="Add responsibility" id="resp_input" ></input><input type="button" value="Add" id="add"> ');
// Add items to input field
var eachline='';
$("#add").click(function(){
var lines = $('#resp_input').val().split('\n');
var lines2 = $('#responsibilities').val().split('\n');
if(lines2.length>10)return false;
for(var i = 0;i < lines.length;i++){
if(lines[i]!='' && i+lines2.length<11){
eachline += lines[i] + '\n';
}
}
$('#responsibilities').text($("<div>" + eachline + "</div>").text()).before("<li>"+$("<p>"+lines+"</p>").text()+"<span class='right'><a href='#'>Remove</a></span></li>");
$('#resp_input').val('');
});
I know people have much more experience, so if anyone is willing to give me any help or advice on how this could be achieved I would really apprecaite it.
Share Improve this question edited Jun 18, 2013 at 20:26 j08691 208k32 gold badges269 silver badges280 bronze badges asked Jun 18, 2013 at 20:22 J.ZilJ.Zil 2,4498 gold badges46 silver badges82 bronze badges2 Answers
Reset to default 6Really simple :
$(document).on('click', '.right a', function(){
$(this).closest('li').remove()
})
Fiddle : http://jsfiddle/ZTuDJ/47/
To remove the textarea too, use this:
$(document).on('click', '.right a', function(){
var el = $(this).closest('li')
var index = $('li').index(el);
var text = eachline.split('\n');
text.splice(index, 1);
eachline = text.join('\n')
$('#responsibilities').text(eachline)
el.remove()
})
Fiddle : http://jsfiddle/ZTuDJ/50/
Something like this?:
$("ul li a").on("click", function(){
$(this).parent().remove();
});
JSFiddle here
本文标签: javascriptRemove item from list using jqueryStack Overflow
版权声明:本文标题:javascript - Remove item from list using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741893411a2403434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论