如何通过更改属性来重新排序我的输入字段?
当我的函数被调用时,如何重新排序我的输入?
即:当单击 a2 删除按钮时,现在我有 a1、a3 和 a4,我想重新排序,使其成为 a1、a2(以前是 a3)和 a3(以前是 a4)。我知道如何手动执行此操作,但如何动态编写呢?
脚本:
$(function(){ $('.deleteMe').click(function(){ $(this) .parent() .parent() .remove(); //rename the attribute ??? $('#a3').attr('name', 'a2') //a3 becomes a2 $('#a4').attr('name', 'a3') //a4 becomes a3 return false; }); });
这是 HTML:
<form name="myform" id="myform" method="post">
<table>
<tr>
<td><input type="text" name="a1" id="a1" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a1" /></td>
</tr>
<tr>
<td><input type="text" name="a2" id="a2" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a2" /></td>
</tr>
<tr>
<td><input type="text" name="a3" id="a3" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a3" /></td>
<tr>
</tr>
<td><input type="text" name="a4" id="a4" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a4" /></td>
</tr>
</table>
</form>
How do I reorder my inputs when my function is called?
ie: When the a2 delete button is clicked now I have a1, a3, and a4 I want to reorder this so that it is a1, a2 (was a3), and a3(was a4). I know how to do this manually, but how do I write it dynamically?
Script:
$(function(){ $('.deleteMe').click(function(){ $(this) .parent() .parent() .remove(); //rename the attribute ??? $('#a3').attr('name', 'a2') //a3 becomes a2 $('#a4').attr('name', 'a3') //a4 becomes a3 return false; }); });
Here is the HTML:
<form name="myform" id="myform" method="post">
<table>
<tr>
<td><input type="text" name="a1" id="a1" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a1" /></td>
</tr>
<tr>
<td><input type="text" name="a2" id="a2" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a2" /></td>
</tr>
<tr>
<td><input type="text" name="a3" id="a3" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a3" /></td>
<tr>
</tr>
<td><input type="text" name="a4" id="a4" /></td>
<td><input type="button" class="deleteMe" name="deleteMe" id="deleteMe" value="a4" /></td>
</tr>
</table>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
class="renameable"
添加到每个输入字段,然后执行如下操作:Add a
class="renameable"
onto each of those input fields, and then do something like this:如果您可以轻松选择所有输入,只需循环遍历所有输入并重命名即可。
然而问题出现了,为什么需要这样做?您可以给所有这些名称指定相同的名称,并且大多数服务器站点的内容应该能够为您提供值列表。
If you can easily select all your inputs, just loop through all of them and rename.
However the question arises, why do you need to do this? You could just give all of them the same name, and most server site stuff should be able to get you a list of values.