简体   繁体   English

如何使用jquery删除名称标签中的元素

[英]How to remove an element in the name tag with jquery

For example, I have an input tag like this:例如,我有一个这样的输入标签:

<input class="" name="abcxyz[full_name]"/>

is there a way to use jquery to remove [full_name] from the name tag?有没有办法使用 jquery 从名称标签中删除 [full_name] ? I want it like this:我想要这样:

<input class="" name="abcxyz"/>

Can someone help me?有人能帮我吗?

Try finding all the inputs with square brackets in their names and then removing the bracketed expressions via the .attr() method尝试查找名称中带有方括号的所有输入,然后通过.attr()方法删除括号中的表达式

 const rx = /\\[.+?\\]/g // for finding and removing "[something]" $("input[name*='[']") .filter((_, { name }) => rx.test(name)) .attr("name", (_, name) => name.replace(rx, "")) // This just shows the result $("#out").text($("#inputs").html())
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="inputs"> <input class="" name="abcxyz[full_name]"/> <input class="" name="abcxyz[full_name"/> <!-- no match --> <input class="" name="abcxyz[full_name][exta]"/> <!-- multiple matches --> </div> <pre id="out"></pre>

The first selector finds all inputs with a [ in the name.第一个选择器查找名称中带有[所有输入。 The filter then makes sure they only match [something] .然后过滤器确保它们只匹配[something]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM