-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-exemplo-radio-checkbox.html
96 lines (80 loc) · 2.35 KB
/
2-exemplo-radio-checkbox.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html>
<head>
<title>Exemplos Javascript não obstrutivo</title>
<!-- ADICIONA CLASSE NO HTML QUANDO HA SUPORTE -->
<script>document.getElementsByTagName('html')[0].className += ' js'</script>
<style type="text/css">
.styleRadio input,
.styleCheckbox input {
cursor:pointer;
filter:alpha(opacity=0);
opacity:0;
}
.styleRadio,
.styleCheckbox {
cursor:pointer;
height:30px;
background:url(controls.gif) no-repeat;
}
.styleRadio {
background-position:0 -500px;
}
.inputRadioChecked {
background-position:-500px -500px;
}
.styleCheckbox {
background-position:0 0;
}
.inputCheckboxChecked {
background-position:-500px 0;
}
.inputFocus {
border:dotted 1px #CCC;
}
</style>
</head>
<body>
<h1>Exemplo javascript não obstrutivo em checkbox e radio personalizados</h1>
<label for="radioButton1"><input type="radio" name="radioButton" id="radioButton1" /> Radio 1</label>
<label for="radioButton2"><input type="radio" name="radioButton" id="radioButton2" /> Radio 2</label>
<br />
<br />
<br />
<label for="checkbox1"><input type="checkbox" name="checkbox" id="checkbox1" /> Checkbox 1</label>
<label for="checkbox2"><input type="checkbox" name="checkbox" id="checkbox2" /> Checkbox 2</label>
<!-- TENTA CARREGAR JQUERY EXTERNO, EM CASO DE TIMEOUT CARREGA LOCAL -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="scripts/jquery-1.7.1.min.js"><\/script>')</script>
<script>
var input = $("input:radio,input:checkbox");
var check = function(obj) {
if (obj.attr("checked")) {
obj.parent().addClass(obj.is(":radio")?"inputRadioChecked":"inputCheckboxChecked")
} else {
obj.parent().removeClass(obj.is(":radio")?"inputRadioChecked":"inputCheckboxChecked")
}
}
// Estado inicial
input.each(function() {
var self = $(this)
if (self.is(":checkbox"))
self.parent().addClass("styleCheckbox")
else if (self.is(":radio"))
self.parent().addClass("styleRadio")
check(self)
});
// Checkbox
input.filter(":checkbox").click(function() {
check($(this))
});
// Radio
input.filter(":radio").click(function() {
var name = $(this).attr("name")
input.filter("input[name='"+name+"']").each(function() {
check($(this))
});
});
</script>
</body>
</html>