如何更改克隆元素的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
更多推荐
发布评论