How to Add / Remove fieldsets with Jquery - better example

We wish to allow our users to add/remove fieldsets according to their needs. Here's the code I've come up with: The jquery part: $("#inputRow1").hide(); $("#inputRow2").hide(); $("#remove-last").hide(); $("#add-more").click(function(){ //count how many *direct children* elements are hidden. var hiddenElements = $('#members >:hidden').length; $("#remove-last").show(); if (hiddenElements === 2) { $("#inputRow1").show(); } else if(hiddenElements === 1) { $("#inputRow2").show(); $(this).hide(); } }); $("#remove-last").click(function(){ //count how many *direct children* elements are hidden. var hiddenElements = $('#members >:hidden').length; $("#add-more").show(); if (hiddenElements === 0) { $("#inputRow"+2).hide(); } else if (hiddenElements === 1) { $("#inputRow"+hiddenElements).hide(); $(this).hide(); } }); The HTML part (server side generated):







add-more remove-last Link: http://jsfiddle.net/URkuW/ I'm aware that his code while it works, it's very naif. Isn't there a *better* / *shorter* / *more compreehsive* / *more re-usable* / way of doing it ? :D
You should post this on Code Review if it already works..

以上就是How to Add / Remove fieldsets with Jquery - better example的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏