mirror of https://github.com/pallets/flask.git
clean up js example
This commit is contained in:
parent
746b91dfce
commit
1e84c67beb
|
@ -163,5 +163,5 @@ explanation of the little bit of code above:
|
||||||
we set earlier.
|
we set earlier.
|
||||||
|
|
||||||
Check out the :gh:`example source <examples/javascript>` for a full
|
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``.
|
thing using ``XMLHttpRequest`` and ``fetch``.
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
<hr>
|
<hr>
|
||||||
<p>{% block intro %}{% endblock %}</p>
|
<p>{% block intro %}{% endblock %}</p>
|
||||||
<hr>
|
<hr>
|
||||||
<form>
|
<form id="calc">
|
||||||
<label>a <input name="a"></label>
|
<label>a <input name="a"></label>
|
||||||
<span>+</span>
|
<span>+</span>
|
||||||
<label>b <input name="b"></label>
|
<label>b <input name="b"></label>
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
<script>
|
<script>
|
||||||
function addSubmit(ev) {
|
function addSubmit(ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
fetch('{{ url_for('add') }}', {
|
fetch({{ url_for('add')|tojson }}, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new FormData(this)
|
body: new FormData(this)
|
||||||
})
|
})
|
||||||
|
@ -30,6 +30,7 @@
|
||||||
span.innerText = data.result;
|
span.innerText = data.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.forms[0].addEventListener('submit', addSubmit);
|
var form = document.getElementById('calc');
|
||||||
|
form.addEventListener('submit', addSubmit);
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '{{ url_for('add') }}',
|
url: {{ url_for('add')|tojson }},
|
||||||
data: $(this).serialize()
|
data: $(this).serialize()
|
||||||
}).done(addShow);
|
}).done(addShow);
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,6 @@
|
||||||
$('#result').text(data.result);
|
$('#result').text(data.result);
|
||||||
}
|
}
|
||||||
|
|
||||||
$('form:first').on('submit', addSubmit);
|
$('#calc').on('submit', addSubmit);
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
request.addEventListener('load', addShow);
|
request.addEventListener('load', addShow);
|
||||||
request.open('POST', '{{ url_for('add') }}');
|
request.open('POST', {{ url_for('add')|tojson }});
|
||||||
request.send(new FormData(this));
|
request.send(new FormData(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@
|
||||||
span.innerText = data.result;
|
span.innerText = data.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.forms[0].addEventListener('submit', addSubmit);
|
var form = document.getElementById('calc');
|
||||||
|
form.addEventListener('submit', addSubmit);
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in New Issue