Advertisement
Guest User

Jquery dynamic select

a guest
Feb 14th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.31 KB | None | 0 0
  1. <html>
  2.     <head>
  3.         <title>Test</title>
  4.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  5.     </head>
  6.     <body>
  7.         <form action="getchar.php" method="POST">
  8.             <select id="selectacc" name="selectacc">
  9.                 <option value="1">Foo</option>
  10.                 <option value="2">Bar</option>
  11.             </select>
  12.             <select id="race" name="race" disabled></select>
  13.         </form>
  14.         <script>
  15.         $(function() {
  16.             $('#selectacc').on('change', function() {
  17.                 var uid = $(this).val();
  18.                 $.post('getchar.php', {id: uid}, function(data) {
  19.                     //Remove all option-tags in #race
  20.                     $('#race').html('');
  21.                     //Make #race selectable
  22.                     $('#race').removeAttr('disabled');
  23.                     //Loop through all items
  24.                     //and adds them to #race
  25.                     $.each(data, function(i) {
  26.                         $('#race')
  27.                             .append($('<option></option>')
  28.                             .attr('value', data[i].id)
  29.                             .text(data[i].name));
  30.                     });
  31.                 });
  32.             });
  33.         });
  34.         </script>
  35.     </body>
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement