-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
114 lines (93 loc) · 3.31 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<html>
<head>
<meta charset="utf-8">
<title>ajax-form element</title>
<!--link rel="stylesheet" href="examples.css"-->
<script type="module" src="src/ajax-form.ts"></script>
</head>
<body>
<script>
function init() {
document.addEventListener( 'ajax-form.result', function( ev ) {
console.dir( ev.detail );
if( !ev.detail.ok) {
alert( ev.detail.statusText )
}
else {
ev.detail.text().then( function(text) {
alert( text );
})
}
})
document.addEventListener( 'ajax-form.error', function( ev ) {
console.dir( ev.detail );
alert( ev.detail.message )
})
var form_redirect = document.getElementById('form-redirect')
if( form_redirect ) {
form_redirect.addEventListener( 'ajax-form.result', function( ev ) {
var res = ev.detail
if( !res.ok) {
alert( res.statusText )
return
}
if( res.redirected ) {
console.log( res.url )
window.location.href = res.url
}
})
}
var form_replace = document.getElementById('form-replace')
if( form_replace ) {
form_replace.addEventListener( 'ajax-form.result', function( ev ) {
var res = ev.detail
if( !res.ok) {
alert( res.statusText )
return
}
res.text().then( function(text) {
document.open()
document.write(text)
document.close()
})
})
}
}
document.addEventListener('DOMContentLoaded', init );
</script>
<h2>ajax-form element demo</h2>
<h3>POST</h3>
<ajax-form >
<form slot="form" action="/api/echo">
<input type="text" name="name" required/>
<select name="options">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>
<input type="submit" text="Submit" />
</form>
</ajax-form>
<h3>POST and redirect</h3>
<ajax-form id="form-redirect" event-bubbles='false' >
<form slot="form" action="/api/echo?redirect=true">
<input type="text" name="name" required/>
<input type="submit" text="Submit" />
</form>
</ajax-form>
<h3>POST and replace</h3>
<ajax-form id="form-replace" event-bubbles='false' >
<form slot="form" action="/api/echo?replace=true">
<input type="text" name="name" required/>
<input type="submit" text="Submit" />
</form>
</ajax-form>
<h3>Programmatically submit</h3>
<ajax-form id="form-sumbit-by-js">
<form slot="form" id="form-sumbit-by-js" action="/api/echo">
<input type="text" name="name" required/>
</form>
</ajax-form>
<a href="#!" onclick="document.getElementById('form-sumbit-by-js').submit()">Submit By Javascript</a>
</body>
</html>