最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript to remove all hidden elements but one - Stack Overflow

programmeradmin7浏览0评论

Someone helped me find JavaScript code to remove hidden form fields from submission and code that ignores a certain field that I don't want removed (whether it's hidden or not):

$("form").submit(function() {
$(this).find(":hidden").remove(); // hide hidden elements before submitting
});

and

:not(input[name=csrfmiddlewaretoken])

However, I can't for the life of me figure out how to put these together. I'm sure it's a basic JavaScript question, but I can't seem to piece these together.

Does anyone know how to remove all hidden form entries not named csrfmiddlewaretoken? If you do, I'd really appreciate it.

Thanks a lot.

Someone helped me find JavaScript code to remove hidden form fields from submission and code that ignores a certain field that I don't want removed (whether it's hidden or not):

$("form").submit(function() {
$(this).find(":hidden").remove(); // hide hidden elements before submitting
});

and

:not(input[name=csrfmiddlewaretoken])

However, I can't for the life of me figure out how to put these together. I'm sure it's a basic JavaScript question, but I can't seem to piece these together.

Does anyone know how to remove all hidden form entries not named csrfmiddlewaretoken? If you do, I'd really appreciate it.

Thanks a lot.

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked Oct 24, 2011 at 4:44 useruser 7,3337 gold badges52 silver badges96 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 11
$(this).find(":hidden").not('input[name=csrfmiddlewaretoken]').remove();

Or

$(this).find(":hidden").filter(':not(input[name=csrfmiddlewaretoken])').remove();

Or

$(this).find("input[name!=csrfmiddlewaretoken]:hidden").remove();
$(this).find(":hidden").filter("[name!='csrfmiddlewaretoken']").remove();

You can pass the this as a context argument, which will be potentially faster than making a jQuery object from it. The :not() expression can follow the :hidden without spaces, meaning that it adds a second condition to the :hidden selector.

$(":hidden:not(input[name=csrfmiddlewaretoken])", this).remove();
发布评论

评论列表(0)

  1. 暂无评论