Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. HTML JavaScript:
  2. ----------------
  3. <script type="text/javascript" src="libs/jquery-checkbox.1.3.0b1/jquery.checkbox.js"></script>
  4. <script type="text/javascript">
  5. $(document).ready(function() {
  6. $('input:checkbox:not([safari])').checkbox();
  7. $('input[safari]:checkbox').checkbox({cls:'jquery-safari-checkbox'});
  8. $('input:radio').checkbox();
  9.  
  10. // $('input:radio').change(function(){alert('callback testi 1')}); // Toimii radiobuttonia painaessa
  11. // $('input:radio').change(displayForm('form1')); // Ei toimi, rikkoo koko rakenteen ja tyylit
  12.  
  13. });
  14.  
  15.  
  16. displayForm = function (elementId)
  17. {
  18. var content = [];
  19. $('#' + elementId + ' input').each(function(){
  20. var el = $(this);
  21. if ( (el.attr('type').toLowerCase() == 'radio'))
  22. {
  23. if ( this.checked )
  24. content.push([
  25. '"', el.attr('name'), '": ',
  26. 'value="', ( this.value ), '"',
  27. ( this.disabled ? ', disabled' : '' )
  28. ].join(''));
  29. }
  30. else
  31. content.push([
  32. '"', el.attr('name'), '": ',
  33. ( this.checked ? 'checked' : 'not checked' ),
  34. ( this.disabled ? ', disabled' : '' )
  35. ].join(''));
  36. });
  37. alert(content.join('\n'));
  38. }
  39.  
  40. changeStyle = function(skin)
  41. {
  42. jQuery('#myform :checkbox').checkbox((skin ? {cls: skin} : {}));
  43. }
  44.  
  45. </script>
  46.  
  47.  
  48.  
  49. HTML Form:
  50. ----------
  51. <div id="form1" style="position:relative" action="urlitus.html" method="POST">
  52. <h3>Default skin</h3>
  53. <p><label><label> <input name="checkbox.1.1" type="checkbox" onclick="var j = jQuery('#check').attr('disabled', jQuery('#check').attr('disabled') ? false : true)"> Unchecked checkbox (by clicking on this checkbox you can disable/enable the checkbox below)</label></label></p>
  54. <p><input name="checkbox.1.2" type="checkbox" id="check" checked> <label for="check"><label> Checked checkbox (this one)</label></label></p>
  55.  
  56. <p><input name="checkbox.1.3" type="checkbox" disabled> Disabled & unchecked checkbox</p>
  57. <p><input name="checkbox.1.4" type="checkbox" disabled checked> Disabled & checked checkbox</p>
  58.  
  59. <h3>Safari skin</h3>
  60. <p><input name="checkbox.2.1" type="checkbox" safari=1 onclick="jQuery('#check2').attr('checked', !jQuery('#check2').attr('checked') ? true : false)"> Unchecked checkbox (by clicking on this checkbox you can check/uncheck the checkbox below)</p>
  61. <p><input name="checkbox.2.2" type="checkbox" safari=1 id="check2" checked> Checked checkbox (this one)</p>
  62.  
  63. <p><input name="checkbox.2.3" type="checkbox" safari=1 disabled> Disabled & unchecked checkbox</p>
  64. <p><input name="checkbox.2.4" type="checkbox" safari=1 disabled checked> Disabled & checked checkbox</p>
  65.  
  66. <h3>Radio button 1 (wrapped in <label>)</h3>
  67. <p><label><input name="radio.1" value="1" type="radio" > 1st radio button</label></p>
  68. <p><label><input name="radio.1" value="2" type="radio" > 2nd radio button</label></p>
  69.  
  70.  
  71. <h3>Radio button 2 (wrapped in <label>)</h3>
  72. <p><label><input name="radio.2" value="1" type="radio" > 1st radio button</label></p>
  73. <p><label><input name="radio.2" value="2" type="radio" > 2nd radio button</label></p>
  74. </div>
  75.  
  76.  
  77.  
  78. jquery.checkbox.js:
  79. -------------------
  80.  
  81. (function($){
  82. /* Little trick to remove event bubbling that causes events recursion */
  83. var CB = function(e)
  84. {
  85. if(!e) var e = window.event;
  86. e.cancelBubble = true;
  87. if(e.stopPropagation) e.stopPropagation();
  88. $ch.change(); // Callback for change event
  89. };
  90.  
  91. $.fn.checkbox = function(options) {
  92. //function(){alert("debug2")};
  93. /* IE6 background flicker fix */
  94. try { document.execCommand('BackgroundImageCache', false, true); } catch (e) {}
  95.  
  96. /* Default settings */
  97. var settings = {
  98. cls: 'jquery-checkbox', /* checkbox */
  99. empty: 'empty.png' /* checkbox */
  100. };
  101.  
  102. /* Processing settings */
  103. settings = $.extend(settings, options || {});
  104.  
  105. /* Adds check/uncheck & disable/enable events */
  106. var addEvents = function(object)
  107. {
  108. var checked = object.checked;
  109. var disabled = object.disabled;
  110. var $object = $(object);
  111.  
  112. if ( object.stateInterval )
  113. clearInterval(object.stateInterval);
  114.  
  115. object.stateInterval = setInterval(
  116. function()
  117. {
  118. if ( object.disabled != disabled )
  119. $object.trigger( (disabled = !!object.disabled) ? 'disable' : 'enable');
  120. if ( object.checked != checked )
  121. $object.trigger( (checked = !!object.checked) ? 'check' : 'uncheck');
  122. },
  123. 10 /* in miliseconds. Low numbers this can decrease performance on slow computers, high will increase responce time */
  124. );
  125. return $object;
  126. };
  127. //try { console.log(this); } catch(e) {}
  128.  
  129. /* Wrapping all passed elements */
  130. return this.each(function()
  131. {
  132. var ch = this; /* Reference to DOM Element*/
  133. var $ch = addEvents(ch); /* Adds custom events and returns, jQuery enclosed object */
  134.  
  135. /* Removing wrapper if already applied */
  136. if (ch.wrapper) ch.wrapper.remove();
  137.  
  138. /* Creating wrapper for checkbox and assigning "hover" event */
  139. ch.wrapper = $('<span class="' + settings.cls + '"><span class="mark"><img src="' + settings.empty + '" /></span></span>');
  140. ch.wrapperInner = ch.wrapper.children('span:eq(0)');
  141. ch.wrapper.hover(
  142. function(e) { ch.wrapperInner.addClass(settings.cls + '-hover');CB(e); },
  143. function(e) { ch.wrapperInner.removeClass(settings.cls + '-hover');CB(e); }
  144. );
  145.  
  146. /* Wrapping checkbox */
  147. $ch.css({position: 'absolute', zIndex: -1, visibility: 'hidden'}).after(ch.wrapper);
  148.  
  149. /* Ttying to find "our" label */
  150. var label = false;
  151. if ($ch.attr('id'))
  152. {
  153. label = $('label[for='+$ch.attr('id')+']');
  154. if (!label.length) label = false;
  155. }
  156. if (!label)
  157. {
  158. /* Trying to utilize "closest()" from jQuery 1.3+ */
  159. label = $ch.closest ? $ch.closest('label') : $ch.parents('label:eq(0)');
  160. if (!label.length) label = false;
  161. }
  162. /* Labe found, applying event hanlers */
  163. if (label)
  164. {
  165. label.hover(
  166. function(e) { ch.wrapper.trigger('mouseover', [e]); },
  167. function(e) { ch.wrapper.trigger('mouseout', [e]); }
  168. );
  169. label.click(function(e) { $ch.trigger('click',[e]); CB(e); return false;});
  170. }
  171. ch.wrapper.click(function(e) { $ch.trigger('click',[e]); CB(e); return false;});
  172. $ch.click(function(e) { CB(e); });
  173. $ch.bind('disable', function() { ch.wrapperInner.addClass(settings.cls+'-disabled');}).bind('enable', function() { ch.wrapperInner.removeClass(settings.cls+'-disabled');});
  174. $ch.bind('check', function() { ch.wrapper.addClass(settings.cls+'-checked' );}).bind('uncheck', function() { ch.wrapper.removeClass(settings.cls+'-checked' );});
  175.  
  176. /* Disable image drag-n-drop for IE */
  177. $('img', ch.wrapper).bind('dragstart', function () {return false;}).bind('mousedown', function () {return false;});
  178.  
  179. /* Firefox antiselection hack */
  180. if ( window.getSelection )
  181. ch.wrapper.css('MozUserSelect', 'none');
  182.  
  183. /* Applying checkbox state */
  184. if ( ch.checked )
  185. ch.wrapper.addClass(settings.cls + '-checked');
  186. if ( ch.disabled )
  187. ch.wrapperInner.addClass(settings.cls + '-disabled');
  188.  
  189. });
  190. }
  191. })(jQuery);
Add Comment
Please, Sign In to add comment