Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dynamiczne dodawanie pól formularza wraz z trescią w value
bluepk
post
Post #1





Grupa: Zarejestrowani
Postów: 12
Pomógł: 0
Dołączył: 20.04.2009

Ostrzeżenie: (0%)
-----


Witajcie!

Mam problem, otoz mam kod, ktory tworzy mi pola w formularzu.
Kod:

<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style type="text/css">
label { display: block;}label a{
color: #CC0000;
text-decoration: none; margin-left: 5px;
}
label a:hover{
text-decoration: underline;
}
</style>
<script>
$(document).ready(
function()
{
$("#dodaj").click(function()
{
var new_label = $("<label>");
var new_input = $("<input>");
new_input.attr("type", "text");
var new_link = $("<a>");
new_link.attr("href", "#");
new_link.html("usuń");
new_link.click(function()
{
$(this).parent("label").remove();
return false;
});
new_label.append(new_input);
new_label.append(new_link);
$("form[name=formularz]").append(new_label);

return false;
});
});
</script>
</head>
<body>

<a href="#" id="dodaj">Dodaj</a>
<form name="formularz" action="">
</form>
</body>


Chce aby bylo wiecej przyciskow Dodaj - np dodaj+tekst, dodaj+tekst2 i zeby ten tekst byl wyswietlany jako value w dodanym polu input. Probowalem dodac value, ale nie moglem tego przekazac z linku do pola.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
reaktor
post
Post #2





Grupa: Zarejestrowani
Postów: 18
Pomógł: 1
Dołączył: 2.01.2010

Ostrzeżenie: (0%)
-----


Wiec w czym problem? Po prostu dorabiaj "name"
podczas produkcji inputow.

Zeby nie bylo wiecej nieporozumien wstawiam caly dziajacy
przyklad. Nie jest to moze bardzo eleganckie, ale robi wszystko
czego potrzebowales.


  1. <!--
  2. <script src="http://code.jquery.com/jquery-latest.js"></script>
  3. -->
  4. <script src="jquery-1.3.2.js" type="text/javascript"></script>
  5.  
  6. <style type="text/css">
  7. label {
  8. display: block;
  9. }
  10. label a{
  11. color: #CC0000;
  12. text-decoration: none; margin-left: 5px;
  13. }
  14. label a:hover{
  15. text-decoration: underline;
  16. }
  17. $(document).ready(function() {
  18. $(".dodaj").click(function()
  19. {
  20. var kolorek = $(this).attr("wrzutadoinputa");
  21.  
  22. var powtorka = false;
  23. var cnt=0;
  24. var cntmax=4;
  25. $("#myform input.autoinput").each(function(){
  26. cnt++;
  27. if (this.id == "autoinput_"+kolorek) {
  28. powtorka = true;
  29. }
  30. });
  31. if (powtorka) {
  32. $("#info").text("Juz wybrales ten kolor");
  33. return;
  34. } else {
  35. $("#info").text("");
  36. }
  37. if (cnt>=cntmax) {
  38. $("#info").text("Mozesz wybrac maksymalnie: " + cntmax);
  39. return;
  40. }
  41.  
  42. var new_label = $("<label>");
  43. var new_input = $("<input>");
  44. new_input.attr("type", "text");
  45. new_input.attr("class", "autoinput");
  46. new_input.attr("id", "autoinput_"+kolorek);
  47.  
  48. new_input.val(kolorek);
  49. new_input.attr("style", "background-color:" + kolorek + ";border:0px;:blue;width:200px;");
  50. //new_input.val(this.title);
  51.  
  52. var new_link = $("<a>");
  53. new_link.attr("href", "#");
  54. new_link.html("usun");
  55. new_link.click(function()
  56. {
  57. $(this).parent("label").remove();
  58. return false;
  59. });
  60.  
  61. new_label.append(new_input);
  62. new_label.append(new_link);
  63. $("form[name=formularz]").append(new_label);
  64.  
  65. return false;
  66. });
  67. });
  68. </head>
  69.  
  70. <a href="#" class="dodaj" wrzutadoinputa="red" onclick="return false">_R_</a>
  71. <a href="#" class="dodaj" wrzutadoinputa="green">_G_</a>
  72. <a href="#" class="dodaj" wrzutadoinputa="blue">_B_</a>
  73. <BR>
  74. <a href="#" class="dodaj" wrzutadoinputa="cyan">_C_</a>
  75. <a href="#" class="dodaj" wrzutadoinputa="magenta">_M_</a>
  76. <a href="#" class="dodaj" wrzutadoinputa="yellow">_Y_</a>
  77. <hr/>
  78. <span id="info"></span>
  79. <BR><BR>
  80.  
  81. <form id="myform" name="formularz" action="">
  82.  
  83. </form>
  84. </body>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 18.10.2025 - 07:02