Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [AJAX]jQuery - wstawienie tekstu do inputa
northwest
post 30.01.2011, 15:33:29
Post #1





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


Witam serdecznie,
Mam taki skrypcik:
  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="css/jquery-ui-1.8.9.custom.css">
  6. <script src="js/jquery-1.4.4.min.js"></script>
  7. <script src="ui/minified/jquery.ui.core.min.js"></script>
  8. <script src="ui/minified/jquery.ui.widget.min.js"></script>
  9. <script src="ui/minified/jquery.ui.mouse.min.js"></script>
  10. <script src="ui/minified/jquery.ui.button.min.js"></script>
  11. <script src="ui/minified/jquery.ui.draggable.min.js"></script>
  12. <script src="ui/minified/jquery.ui.position.min.js"></script>
  13. <script src="ui/minified/jquery.ui.resizable.min.js"></script>
  14. <script src="ui/minified/jquery.ui.dialog.min.js"></script>
  15. <script src="ui/minified/jquery.effects.core.min.js"></script>
  16. <script src="ui/minified/jquery.effects.blind.min.js"></script>
  17. <script src="ui/minified/jquery.effects.explode.min.js"></script>
  18. body { font-size: 62.5%; }
  19. label, input { display:block; }
  20. input.text { margin-bottom:12px; width:95%; padding: .4em; }
  21. fieldset { padding:0; border:0; margin-top:25px; }
  22. h1 { font-size: 1.2em; margin: .6em 0; }
  23. div#users-contain { width: 350px; margin: 20px 0; }
  24. div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
  25. div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
  26. .ui-dialog .ui-state-error { padding: .3em; }
  27. .validateTips { border: 1px solid transparent; padding: 0.3em; }
  28. </style>
  29. <script>
  30.  
  31.  
  32.  
  33.  
  34.  
  35. $("#close-button").click(function() {
  36. $(this).oneTime(1000, function() {
  37. $(this).parent(".main-window").hide();
  38. });
  39. });
  40. $("#cancel-button").click(function() {
  41. $("#close-button").stopTime();
  42. });
  43. // increase the default animation speed to exaggerate the effect
  44. $.fx.speeds._default = 500;
  45. $(function() {
  46. $( "#dialog" ).dialog({
  47. autoOpen: false,
  48. height: 240,
  49. width: 240,
  50. show: "explode",
  51. modal: true,
  52. hide: "explode",
  53. position: ['center','center'],
  54. buttons: {
  55. Ok: function() {
  56. $( this ).dialog( "close" );
  57. $(this).dialog('destroy');
  58. }
  59. }
  60. });
  61.  
  62. $( "#opener" ).click(function() {
  63. $("#dialog").dialog( "open" );
  64.  
  65. return false;
  66. });
  67.  
  68.  
  69. $( "#kasownik" ).click(function() {
  70. $("#dialog-confirm").dialog( "open" );
  71. return false;
  72. });
  73. });
  74. </script>
  75.  
  76.  
  77.  
  78. function deleteconfirmed(id){
  79. $.post('dane.php',{pokaz:'users'},function(odebrane_dane){
  80. $('#users').html(odebrane_dane)
  81. })
  82. setTimeout("odswiez_diva()", 1000);
  83. }
  84.  
  85. function odswiez_diva(){
  86. $.post('dane.php',{pokaz:'users'},function(odebrane_dane){
  87. $('#users').html(odebrane_dane)
  88. })
  89. setTimeout("odswiez_diva()", 1000);
  90. }
  91.  
  92. $(function() {
  93. // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
  94. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  95.  
  96. var name = $( "#name" ),
  97. email = $( "#email" ),
  98. password = $( "#password" ),
  99. allFields = $( [] ).add( name ).add( email ).add( password ),
  100. tips = $( ".validateTips" );
  101.  
  102. function updateTips( t ) {
  103. tips
  104. .text( t )
  105. .addClass( "ui-state-highlight" );
  106. setTimeout(function() {
  107. tips.removeClass( "ui-state-highlight", 1500 );
  108. }, 500 );
  109. }
  110.  
  111. function checkLength( o, n, min, max ) {
  112. if ( o.val().length > max || o.val().length < min ) {
  113. o.addClass( "ui-state-error" );
  114. updateTips( "Length of " + n + " must be between " +
  115. min + " and " + max + "." );
  116. return false;
  117. } else {
  118. return true;
  119. }
  120. }
  121.  
  122. function checkRegexp( o, regexp, n ) {
  123. if ( !( regexp.test( o.val() ) ) ) {
  124. o.addClass( "ui-state-error" );
  125. updateTips( n );
  126. return false;
  127. } else {
  128. return true;
  129. }
  130. }
  131.  
  132. $( "#dialog-form" ).dialog({
  133. autoOpen: false,
  134. height: 300,
  135. width: 350,
  136. modal: true,
  137. buttons: {
  138. "Create an account": function() {
  139. var bValid = true;
  140. allFields.removeClass( "ui-state-error" );
  141.  
  142. bValid = bValid && checkLength( name, "username", 3, 16 );
  143. bValid = bValid && checkLength( email, "email", 6, 80 );
  144. bValid = bValid && checkLength( password, "password", 5, 16 );
  145.  
  146. bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
  147. // From jquery.validate.js (by joern), contributed by Scott Gonzalez: <a href="http://projects.scottsplayground.com/email_address_validation/" target="_blank">http://projects.scottsplayground.com/email...ess_validation/</a>
  148. bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
  149. bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
  150. name
  151. if ( bValid ) {
  152. $.post('test.php', { 'name' : $('#name').val(),'email' : $('#email').val(),'send': 'ok'});
  153.  
  154. $( this ).dialog( "close" );
  155. var editor = $('#dialog-form textarea').ckeditorGet();
  156. editor.destroy();
  157. $(this).dialog('destroy');
  158. }
  159. },
  160. Cancel: function() {
  161. $( this ).dialog( "close" );
  162. }
  163. },
  164. open: function(){
  165. var ckoptions = { langCode: 'pl', skin : 'kama',
  166. width:600,
  167. toolbar:
  168. [
  169. ['Source', '-', 'Bold', 'Italic', 'Underline', 'Strike', 'LeftJustify' ],
  170. ['Image', 'Link', 'Unlink', 'Anchor'],
  171. ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Find','Replace'],
  172. ['Undo','Redo','-','SelectAll','RemoveFormat'],
  173. ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
  174. ['NumberedList','BulletedList'],
  175. ['Format'],
  176. ['TextColor','BGColor'],
  177. ['Font','FontSize'],
  178. ]
  179. }
  180. //$(this).append('<textarea class="editor"></textarea>');
  181. $(this).find('textarea.editor').ckeditor(ckoptions); //ckoptions
  182. },
  183. close: function() {
  184. allFields.val( "" ).removeClass( "ui-state-error" );
  185. $("#users").toggle('slow');
  186. //$('#users').load('slow').load();
  187. //$("#users").load("dane.php");
  188. //$("#users").toggle('slow');
  189. odswiez_diva();$("#users").toggle('slow');
  190. $(this).dialog('destroy');
  191. }
  192. });
  193.  
  194. $( "#create-user" )
  195. .button()
  196. .click(function() {
  197.  
  198. //$('#dialog-form').load('index.php?id='+id);
  199. $( "#dialog-form" ).append( "<p>" +
  200. "<b>" + name.val() + "</b>" +
  201. "<b>" + email.val() + "</b>" +
  202. "<b>" + password.val() + "</b>" +
  203. "</p>" ); $( "#dialog-form" ).dialog( "open" );
  204. //$('#dialog-form textarea').ckeditor();
  205. });
  206.  
  207. $( "#dialog" ).dialog({
  208. autoOpen: false,
  209. height: 240,
  210. width: 240,
  211. show: "explode",
  212. modal: true,
  213. hide: "explode"
  214. });
  215.  
  216. $( "#dialog-confirm" ).dialog({
  217. autoOpen: false,
  218. resizable: false,
  219. height:140,
  220. modal: true,
  221. buttons: {
  222. "Delete all items": function() {
  223. $("#users").toggle('slow');
  224. deleteconfirmed();$("#users").toggle('slow');
  225. $( this ).dialog( "close" );
  226. },
  227. Cancel: function() {
  228. $( this ).dialog( "close" );
  229. }
  230. }
  231. });
  232.  
  233. });
  234. </script>
  235. </head>
  236. <body>
  237.  
  238. <div class="demo">
  239. <button id="create-user">Create new user</button>
  240.  
  241.  
  242.  
  243. <div id="dialog-form" title="Create new user">
  244. <p class="validateTips">All form fields are required.</p>
  245.  
  246. <form method = "post">
  247. <input type="text" name="id-produktu" value="">
  248. <label for="name">Name</label>
  249. <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
  250. <label for="email">Email</label>
  251. <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
  252. <label for="password">Password</label>
  253. <textarea name="password" id="password" value="" class="editor"></textarea>
  254. </form>
  255. </div>
  256. </div>
  257.  


Po kliknięciu na button wyskakuje mi okienko - i tak ma być. Chciałbym jakoś za pomocą tego buttona uzupełnić
inputa: <input type="text" name="id-produktu" value=""> jakąs dowolną wartością ( np. "mój kod").

Czyli: klikamy buttona i wyskakuje nam okienko z uzupełnionym inputem. Teraz wszystko jest ok, ale input
jest pusty....

Wie ktoś może jak to zrobićquestionmark.gif


Z góry dziękuje za pomoc,
Northwest
Go to the top of the page
+Quote Post
Mlodycompany
post 30.01.2011, 15:51:51
Post #2





Grupa: Zarejestrowani
Postów: 910
Pomógł: 44
Dołączył: 20.02.2008
Skąd: Łódź

Ostrzeżenie: (20%)
X----


[JAVASCRIPT] pobierz, plaintext
  1. $('element').val('wartosc')
[JAVASCRIPT] pobierz, plaintext

lub
[JAVASCRIPT] pobierz, plaintext
  1. $('element').attr('value', 'wartosc')
[JAVASCRIPT] pobierz, plaintext



Ten post edytował Mlodycompany 30.01.2011, 15:52:36
Go to the top of the page
+Quote Post
northwest
post 30.01.2011, 16:05:41
Post #3





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


kurcze,nie działa sad.gif
zrobiłem:
  1. $( "#create-user" )
  2. .button()
  3. .click(function() {
  4. $( "#dialog-form" ).dialog( "open" );
  5. $('#dialog-form').attr('id-produktu', 'wartosc');
  6. });


i nie działa:(
Go to the top of the page
+Quote Post
Mlodycompany
post 30.01.2011, 16:16:10
Post #4





Grupa: Zarejestrowani
Postów: 910
Pomógł: 44
Dołączył: 20.02.2008
Skąd: Łódź

Ostrzeżenie: (20%)
X----


wystarczy zajrzeć do manuala jQuery i poszukać. Nie działa bo masz błąd w 5 linii w/w kodu. Otóż funkcja attr() nadaje elementowi html jakies atrybuty. Funkcja ta przyjmuje 2 argumenty - nazwę atrybutu w elemencie html i wartość tego atrybutu. Np.
  1. <input type="submit" id="submit" value="">

[JAVASCRIPT] pobierz, plaintext
  1. $('#submit').attr('value', 'Przycisk');
[JAVASCRIPT] pobierz, plaintext
Go to the top of the page
+Quote Post
northwest
post 30.01.2011, 16:29:36
Post #5





Grupa: Zarejestrowani
Postów: 788
Pomógł: 1
Dołączył: 17.09.2004

Ostrzeżenie: (10%)
X----


działa, dzięki WIELKIE:)))
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 20.04.2024 - 00:06