clean up js example

This commit is contained in:
David Lord 2018-04-12 12:17:14 -07:00
parent 746b91dfce
commit 1e84c67beb
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
5 changed files with 10 additions and 8 deletions

View File

@ -163,5 +163,5 @@ explanation of the little bit of code above:
we set earlier.
Check out the :gh:`example source <examples/javascript>` for a full
application demonstrating the jQuery on this page, as well as the same
application demonstrating the code on this page, as well as the same
thing using ``XMLHttpRequest`` and ``fetch``.

View File

@ -23,7 +23,7 @@
<hr>
<p>{% block intro %}{% endblock %}</p>
<hr>
<form>
<form id="calc">
<label>a <input name="a"></label>
<span>+</span>
<label>b <input name="b"></label>

View File

@ -13,7 +13,7 @@
<script>
function addSubmit(ev) {
ev.preventDefault();
fetch('{{ url_for('add') }}', {
fetch({{ url_for('add')|tojson }}, {
method: 'POST',
body: new FormData(this)
})
@ -30,6 +30,7 @@
span.innerText = data.result;
}
document.forms[0].addEventListener('submit', addSubmit);
var form = document.getElementById('calc');
form.addEventListener('submit', addSubmit);
</script>
{% endblock %}

View File

@ -13,7 +13,7 @@
ev.preventDefault();
$.ajax({
method: 'POST',
url: '{{ url_for('add') }}',
url: {{ url_for('add')|tojson }},
data: $(this).serialize()
}).done(addShow);
}
@ -22,6 +22,6 @@
$('#result').text(data.result);
}
$('form:first').on('submit', addSubmit);
$('#calc').on('submit', addSubmit);
</script>
{% endblock %}

View File

@ -12,7 +12,7 @@
ev.preventDefault();
var request = new XMLHttpRequest();
request.addEventListener('load', addShow);
request.open('POST', '{{ url_for('add') }}');
request.open('POST', {{ url_for('add')|tojson }});
request.send(new FormData(this));
}
@ -22,6 +22,7 @@
span.innerText = data.result;
}
document.forms[0].addEventListener('submit', addSubmit);
var form = document.getElementById('calc');
form.addEventListener('submit', addSubmit);
</script>
{% endblock %}