如何更改克隆元素的ID和名称?(how to change ids and name of cloned elements?)

编程入门 行业动态 更新时间:2024-10-28 13:14:13
如何更改克隆元素的ID和名称?(how to change ids and name of cloned elements?)

我做了一个选择框,我使用克隆来制作动态选择框,但我的克隆选择框ID和名称是如何更改克隆元素的ID和名称相同?这是我的代码:

<script type="text/javascript"> ( function($) { // we can now rely on $ within the safety of our "bodyguard" function $(document).ready( function() { $('#add_button').on('click', function() { var selectClone = $('#event_2').clone(true); // make a copy of select $('#some_target').append(selectClone); // append to clone select }); }); }) ( jQuery ); </script> <? $strQuery1="SELECT event_id,event_name from events"; $result1 = $GLOBALS ['mysqli']->query ($strQuery1) or die ($GLOBALS ['mysqli']->error . __LINE__); ?> <select id="event_2" name="event_2"> <? while($ors1 = $result1->fetch_assoc()) { echo '<option value="'.$ors1['event_id'] .'" >' . $ors1['event_name'] . '</option>'; } ?> </select> <button type="button" value="Add" id="add_button">Add Events</button><br /> <div id="some_target"> </div>

我想下一个克隆元素有id和名称event_3等等我怎么做?

i make a select box and i used clone to make dynamic select boxes but my cloned select boxes ids and names are the same how to change ids and name of cloned elements?here is my code:

<script type="text/javascript"> ( function($) { // we can now rely on $ within the safety of our "bodyguard" function $(document).ready( function() { $('#add_button').on('click', function() { var selectClone = $('#event_2').clone(true); // make a copy of select $('#some_target').append(selectClone); // append to clone select }); }); }) ( jQuery ); </script> <? $strQuery1="SELECT event_id,event_name from events"; $result1 = $GLOBALS ['mysqli']->query ($strQuery1) or die ($GLOBALS ['mysqli']->error . __LINE__); ?> <select id="event_2" name="event_2"> <? while($ors1 = $result1->fetch_assoc()) { echo '<option value="'.$ors1['event_id'] .'" >' . $ors1['event_name'] . '</option>'; } ?> </select> <button type="button" value="Add" id="add_button">Add Events</button><br /> <div id="some_target"> </div>

i want next cloned element has id and name event_3 and so on how i do this?

最满意答案

这将照顾一切:

$('#secondevent').clone().attr('id', 'newid').attr('name', 'newname').appendTo( "#some_target" );

克隆,分配新id,分配新名称和apppend到元素#some_target 。

小提琴

This will take care of everything:

$('#secondevent').clone().attr('id', 'newid').attr('name', 'newname').appendTo( "#some_target" );

clone, assign new id, assign new name and apppend to element #some_target.

Fiddle

更多推荐

本文发布于:2023-08-03 15:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   元素   名称   cloned   elements

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!