Replace input value .val() with jQuery

So basically here is my jsFiddle - http://jsfiddle.net/CmNFu/ . And code also here - HTML - category 1
category 2


​ jQuery - jQuery(document).ready(function() { jQuery(".portfolio-category").click(function() { if(jQuery(this).is(":checked")) { jQuery("#portfolio-categories").val(jQuery("#portfolio-categories").val()+" "+jQuery(this).val()); } else { var portfolioCategories = jQuery("#portfolio-categories").val(); alert("before + "+portfolioCategories); var currentElement = jQuery(this).val()+" "; alert(currentElement); portfolioCategories = portfolioCategories.replace(currentElement, ""); alert(portfolioCategories); } }); }); ​Well basically what I would like to achieve is, when user checks the checkbox, the value automatically adds inside input field (Done, it's working, whooray!), but the problem is when it unchecks the checkbox, the value should be removed from input box (the problem starts here), it doesn't remove anything. You can see I tried assigning val() function to variables, but also without success. Check my example on jsFiddle to see it live. Any suggestions? I guess replace() is not working for val(), is it? So, is there any other suggestions?

以上就是Replace input value .val() with jQuery的详细内容,更多请关注web前端其它相关文章!

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

评论 抢沙发

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

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

联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏