Ver Mensaje Individual
  #6 (permalink)  
Antiguo 23/11/2012, 12:04
Avatar de Javier01
Javier01
 
Fecha de Ingreso: febrero-2008
Ubicación: Montevideo
Mensajes: 261
Antigüedad: 16 años, 2 meses
Puntos: 31
Respuesta: clonar combobox junto con cajas de texto

Bueno, veamos si entendí:
El problema es que al clonar los campos, los select de las horas y los minutos no quedan en blanco?
Porque he probado el código y me funciona perfectamente. Al clonar los campos todos quedan en blanco, y puedo seleccionar las horas y minutos.
Posteo el código que yo probé, puesto que tu usas algunas funciones que yo no tengo:

Código PHP:
Ver original
  1. <!html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  5.         <title>Titulo</title>
  6.             <script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>
  7.     </head>
  8.     <body>
  9.         <div id="medicamento">
  10. <div class="tt">Medicamento programado</div>
  11. <div class="nametx"><?php echo "javier"; ?></div>
  12. <form id="myForm" method="post" name="forma" action="medicamento.php?accion=s" onSubmit="return checkform(this);" enctype="multipart/form-data">
  13. <input type="hidden" id="num_campos" name="num_campos" value="0" />
  14. <input type="hidden" id="cant_campos" name="cant_campos" value="0" />
  15. <input name="aid" type="hidden" onkeypress="javascript: return utils.isString(event);" value="<?php echo "1"; ?>">
  16. <div id="input1" style="margin-bottom: 15px;" class="clonedInput">
  17. <table cellpadding="0" cellspacing="2" border="0">
  18. <tr>
  19. <td>
  20. Nombre
  21. </td>
  22. <td>
  23. Dosis
  24. </td>
  25. <td width="100">
  26. Hora
  27. </td>
  28. <td width="100">
  29. Hora
  30. </td>
  31. <td width="100">
  32. Hora
  33. </td>
  34. <td width="100">
  35. Hora
  36. </td>
  37. </tr>
  38. <tr>
  39. <td>
  40. <input id="reporte_medicamento_medicamento_nombre" type="text" name="reporte_medicamento_medicamento1_nombre" onkeypress="javascript: return utils.isString(event);" value="" style="width: 100px;">
  41. </td>
  42. <td>
  43. <input id="reporte_medicamento_medicamento_dosis" type="text" name="reporte_medicamento_medicamento1_dosis" onkeypress="javascript: return utils.isString(event);" value="" style="width: 25px;">
  44. </td>
  45. <td>
  46. <select id="reporte_medicamento_medicamento_hora1_hr" name="reporte_medicamento_medicamento1_hora1_hr" class="styled">
  47. <option selected value="00">00</option>
  48. <?php
  49. for ($i=1; $i<=24; $i++)
  50. {
  51. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  52. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  53. echo $corrigecero.$i;
  54. }
  55. ?>
  56.  
  57. </select>&nbsp;:&nbsp;
  58. <select id="reporte_medicamento_medicamento_hora1_min" name="reporte_medicamento_medicamento1_hora1_min" class="styled">
  59. <option selected value="00">00</option>
  60. <?php
  61. for ($i=1; $i<=59; $i++)
  62. {
  63. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  64. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  65. }
  66. ?>
  67. </select>
  68. </td>
  69. <td>
  70. <select id="reporte_medicamento_medicamento_hora2_hr" name="reporte_medicamento_medicamento1_hora2_hr" class="styled">
  71. <option selected value="00">00</option>
  72. <?php
  73. for ($i=1; $i<=24; $i++)
  74. {
  75. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  76. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  77. }
  78. ?>
  79. </select>&nbsp;:&nbsp;
  80. <select id="reporte_medicamento_medicamento_hora2_min" name="reporte_medicamento_medicamento1_hora2_min" class="styled">
  81. <option selected value="00">00</option>
  82. <?php
  83. for ($i=1; $i<=59; $i++)
  84. {
  85. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  86. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  87. }
  88. ?>
  89. </select>
  90. </td>
  91. <td>
  92. <select id="reporte_medicamento_medicamento_hora3_hr" name="reporte_medicamento_medicamento1_hora3_hr" class="styled">
  93. <option selected value="00">00</option>
  94. <?php
  95. for ($i=1; $i<=24; $i++)
  96. {
  97. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  98. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  99. }
  100. ?>
  101. </select>&nbsp;:&nbsp;<select id="reporte_medicamento_medicamento_hora3_min" name="reporte_medicamento_medicamento1_hora3_min" class="styled">
  102. <option selected value="00">00</option>
  103. <?php
  104. for ($i=1; $i<=59; $i++)
  105. {
  106. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  107. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  108. }
  109. ?>
  110. </select>
  111. </td>
  112. <td>
  113. <select id="reporte_medicamento_medicamento_hora4_hr" name="reporte_medicamento_medicamento1_hora4_hr" class="styled">
  114. <option selected value="00">00</option>
  115. <?php
  116. for ($i=1; $i<=24; $i++)
  117. {
  118. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  119. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  120. }
  121. ?>
  122. </select>&nbsp;:&nbsp;<select id="reporte_medicamento_medicamento_hora4_min" name="reporte_medicamento_medicamento1_hora4_min" class="styled">
  123. <option selected value="00">00</option>
  124. <?php
  125. for ($i=1; $i<=59; $i++)
  126. {
  127. if ($i<10) { $corrigecero='0'; } else { $corrigecero=''; }
  128. echo '<option value="'.$corrigecero.$i.'">'.$corrigecero.$i.'</option>';
  129. }
  130. ?>
  131. </select>
  132. </td>
  133. </tr>
  134. </table>
  135. </div>
  136. <div id="agregar_eliminar_btns">
  137. <input type="button" id="btnAdd" value=" + " onclick="" />
  138. <input type="button" id="btnDel" value=" - " />
  139. </div>
  140. <table>
  141. <tr>
  142. <td colspan="6">
  143. Observaciones<br />
  144. <textarea id="reporte_medicamento_medicamento1_observaciones " name="reporte_medicamento_medicamento1_observacion es" rows="2" cols="" style="width: 550px;"></textarea>
  145. </td>
  146. </tr>
  147. </table>
  148. <input name="Guardar" type="submit" id="Guardar" value="Guardar">
  149. </form>
  150. </div>
  151.  
  152.  
  153.     <script type="text/javascript">
  154. //cambia los name de los inputs
  155. actualizaName = function ($input)
  156. {
  157.     iNombre = '#reporte_medicamento_medicamento_nombre';
  158.     iDosis = '#reporte_medicamento_medicamento_dosis';
  159.     shora1 = '#reporte_medicamento_medicamento_hora1_hr';
  160.     sminutos1 = '#reporte_medicamento_medicamento_hora1_min';
  161.     shora2 = '#reporte_medicamento_medicamento_hora2_hr';
  162.     sminutos2 = '#reporte_medicamento_medicamento_hora2_min';
  163.     shora3 = '#reporte_medicamento_medicamento_hora3_hr';
  164.     sminutos3 = '#reporte_medicamento_medicamento_hora3_min';
  165.     shora4 = '#reporte_medicamento_medicamento_hora4_hr';
  166.     sminutos4 = '#reporte_medicamento_medicamento_hora4_min';
  167.  
  168.     for(var i = 1; i <= $input ; i++)
  169.     {
  170.         Nombre = 'reporte_medicamento_medicamento';
  171.         Dosis = 'reporte_medicamento_medicamento';
  172.         hora1 = 'reporte_medicamento_medicamento';
  173.         minutos1 = 'reporte_medicamento_medicamento';
  174.         hora2 = 'reporte_medicamento_medicamento';
  175.         minutos2 = 'reporte_medicamento_medicamento';
  176.         hora3 = 'reporte_medicamento_medicamento';
  177.         minutos3 = 'reporte_medicamento_medicamento';
  178.         hora4 = 'reporte_medicamento_medicamento';
  179.         minutos4 = 'reporte_medicamento_medicamento';
  180.  
  181.         Nombre += i + "_nombre";
  182.         Dosis += i + "_dosis";
  183.         hora1 += i + "_hora1_hr";
  184.         minutos1 += i + "_hora1_min";
  185.         hora2 += i + "_hora2_hr";
  186.         minutos2 += i +"_hora2_min";
  187.         hora3 += i +"_hora3_hr";
  188.         minutos3 += i +"_hora3_min";
  189.         hora4 += i +"_hora4_hr";
  190.         minutos4 += i +"_hora4_min";
  191.  
  192.         var input = $('#input'+i);
  193.  
  194.         input.find(iNombre).attr("name",Nombre);
  195.         input.find(iDosis).attr("name",Dosis);
  196.         input.find(shora1).attr("name",hora1);
  197.         input.find(sminutos1).attr("name",minutos1);
  198.         input.find(shora2).attr("name",hora2);
  199.         input.find(sminutos2).attr("name",minutos2);
  200.         input.find(shora3).attr("name",hora3);
  201.         input.find(sminutos3).attr("name",minutos3);
  202.         input.find(shora4).attr("name",hora4);
  203.         input.find(sminutos4).attr("name",minutos4);
  204.  
  205.     }
  206. }
  207. $(document).ready(function() {
  208.  $('#btnAdd').click(function()
  209. {
  210. var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
  211. var newNum = new Number(num + 1); // the numeric ID of the new input field being added
  212. iNombre = '#reporte_medicamento_medicamento_nombre';
  213. iDosis = '#reporte_medicamento_medicamento_dosis';
  214. shora1= '#reporte_medicamento_medicamento1_hora1_hr';
  215. sminutos1='#reporte_medicamento_medicamento1_hora1 _min';
  216. shora2 = '#reporte_medicamento_medicamento_hora2_hr';
  217. sminutos2 = '#reporte_medicamento_medicamento_hora2_min';
  218. shora3 = '#reporte_medicamento_medicamento_hora3_hr';
  219. sminutos3 = '#reporte_medicamento_medicamento_hora3_min';
  220. shora4 = '#reporte_medicamento_medicamento_hora4_hr';
  221. sminutos4 = '#reporte_medicamento_medicamento_hora4_min';
  222.  
  223.  
  224. // create the new element via clone(), and manipulate it's ID using newNum value
  225. var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
  226. var strNewElement = newElem.find('input').text();
  227. // manipulate the name/id values of the input inside the new element
  228. newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
  229. // insert the new element after the last "duplicatable" input field
  230. //limpia el texto del nuevo input
  231. $('#input' + num).after(newElem);
  232. $('#input' + newNum).find(iNombre).attr("value","");
  233. $('#input' + newNum).find(iDosis).attr("value","");
  234. $('#input' + newNum).find(shora1).attr("value","");
  235.  
  236. // enable the "remove" button
  237. $('#btnDel').removeAttr("disabled");
  238. // business rule: you can only add 4 names
  239. if (newNum == 4)
  240. $('#btnAdd').attr('disabled','disabled');
  241.  
  242. //cambia los nombres de los nuevos inputs y selects
  243. actualizaName(newNum);
  244. });
  245.  
  246. $('#btnDel').click(function() {
  247. var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
  248. $('#input' + num).remove(); // remove the last element
  249. // enable the "add" button
  250. $('#btnAdd').removeAttr("disabled");
  251. // if only one element remains, disable the "remove" button
  252. if (num-1 == 1)
  253. $('#btnDel').attr('disabled','disabled');
  254. });
  255. $('#btnDel').attr('disabled','disabled');
  256. });
  257. </script>
  258.  
  259.     </body>
  260. </html>
__________________
Tomarse un tiempo para redactar correctamente la pregunta, utilizando los signos de puntuación adecuados, es ganar tiempo y calidad en la respuesta.