Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 5.18 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Button onClick event triggered too many times in a jQuery Mobile Dialog
  2. <!doctype html>
  3. <html>
  4.  
  5. <head>
  6.     <title>Problem with Dialog re-use</title>
  7.     <meta name="viewport" content="width=device-width, initial-scale=1">
  8.     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
  9.     <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
  10.     <script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
  11. </head>
  12.  
  13. <body>
  14.     <div data-role="page">
  15.         <div data-role="header">
  16.             <h1>Problem with Dialog re-use</h1>
  17.         </div>
  18.         <div data-role="content">
  19.             <p><h2>Activated by</h2></p>
  20.             <ul id="activate_ul">
  21.                 <!-- li to be added dynamically-->
  22.             </ul>
  23.             <a href="javascript:addPopup()" data-rel="popup" data-role="button">Add</a>    
  24.         </div>
  25.     </div>    
  26.     <div id="myDialog" data-role="dialog">    
  27.         <div data-role="header" data-theme="d">
  28.             <h1>My Elements</h1>
  29.         </div>    
  30.         <div id="myDialogContent" data-role="content" data-theme="c">
  31.             <p>Element Type</p>
  32.             <select id="element-type">
  33.                 <!-- options to be added dynamically-->
  34.             </select>
  35.             <p>Element Name</p>
  36.             <select id="element-list">
  37.                 <!-- options to be added dynamically-->
  38.             </select>
  39.             <a href="#" id="OkButtonPopup" data-role="button" data-rel="back" data-theme="b">Ok</a>
  40.         </div>
  41.     </div>
  42.     <script type="text/javascript">
  43.         var all_inputs = ["myInput1","myInput2","myInput3","myInput4"];
  44.         var all_outputs = ["myOutput1","myOutput2","myOutput3","myOutput4"];
  45.         var all_zones = ["myZone1","myZone2","myZone3","myZone4"];
  46.  
  47.         function updateInputList() {
  48.             $('#element-list').empty();        
  49.             for (var i = 0; i < all_inputs.length; i++ ) {
  50.                 $('#element-list').append('<option value="'+all_inputs[i]+'">'+all_inputs[i]+'</option>');
  51.             }
  52.         }
  53.  
  54.         function updateOutputList() {
  55.             $('#element-list').empty();
  56.             for (var i = 0; i < all_outputs.length; i++ ) {
  57.                 $('#element-list').append('<option value="'+all_outputs[i]+'">'+all_outputs[i]+'</option>');
  58.             }
  59.         }
  60.  
  61.         function updateZoneList() {
  62.             $('#element-list').empty();
  63.             for (var i = 0; i < all_zones.length; i++ ) {
  64.                 $('#element-list').append('<option value="'+all_zones[i]+'">'+all_zones[i]+'</option>');
  65.             }
  66.         }
  67.  
  68.         function removeByValue(arr, val) {
  69.             for(var i=0; i<arr.length; i++) {
  70.                 if(arr[i] == val) {
  71.                     arr.splice(i, 1);
  72.                     break;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         function addPopup() {
  78.             $('#element-type').empty();
  79.             $('#element-type').append('<option value="Input">Input</option>')
  80.                             .append('<option value="Output">Ouput</option>')
  81.                             .append('<option value="Zone">Zone</option>');    
  82.  
  83.             updateInputList();
  84.  
  85.             $('#element-type').change();        
  86.  
  87.             $('#element-type').on("change", function() {
  88.                 if ($("option:selected", this).attr('value') == "Input") {
  89.                     updateInputList();
  90.                 }
  91.                 if ($("option:selected", this).attr('value') == "Output") {
  92.                     updateOutputList();
  93.                 }
  94.                 if ($("option:selected", this).attr('value') == "Zone") {
  95.                     updateZoneList();
  96.                 }
  97.  
  98.                 $('#element-list').selectmenu('refresh');
  99.             });
  100.  
  101.             // Event triggered too many times!! Why???
  102.             $('#OkButtonPopup').on("click", function() {
  103.                 $('#activate_ul').append('<li>' + $('#element-list').val() +'</li>');
  104.  
  105.                 // remove element from corresponding array
  106.                 if ($('#element-type').val() == 'Input' ) {
  107.                     removeByValue(all_inputs, $('#element-list').val());
  108.                 }
  109.                 if ($('#element-type').val() == 'Output' ) {
  110.                     removeByValue(all_outputs, $('#element-list').val());
  111.                 }
  112.                 if ($('#element-type').val() == 'Zone' ) {
  113.                     removeByValue(all_zones, $('#element-list').val());
  114.                 }
  115.             });
  116.  
  117.             $.mobile.changePage("#myDialog", { role: "dialog" });
  118.         }
  119.     </script>
  120. </body>
  121. </html>
  122.        
  123. $('#OkButtonPopup').unbind("click").bind("click", function() {
  124.                 $('#activate_ul').append('<li>' + $('#element-list').val() +'</li>');
  125.  
  126.                 // remove element from corresponding array
  127.                 if ($('#element-type').val() == 'Input' ) {
  128.                     removeByValue(all_inputs, $('#element-list').val());
  129.                 }
  130.                 if ($('#element-type').val() == 'Output' ) {
  131.                     removeByValue(all_outputs, $('#element-list').val());
  132.                 }
  133.                 if ($('#element-type').val() == 'Zone' ) {
  134.                     removeByValue(all_zones, $('#element-list').val());
  135.                 }
  136.             });