如何在加载页面和提交表单时调用函数
How to call function when load page and when submit form?
加载页面和提交表单时如何调用函数?
当我加载页面时,我想调用函数function xxx()
当我提交表格时,我想再次致电function xxx()
,我该怎么做?
NOTE: this example code call `function xxx()` when submit form only but not call `function xxx()` when load page
index.php.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<form method="post" id="f1">
<input type="checkbox" id="one" name="number" value="one" onclick="xxx()">1<br>
<input type="checkbox" id="two" name="number" value="two" onclick="xxx()">2<br>
<input type="checkbox" id="three" name="number" value="three" onclick="xxx()">3<br>
</form>
<div id="overlay" style="position: absolute; width: 100%; height: 100%; top: 0px; left: 0px; text-align: center; padding-top: 25%; cursor: wait; font-size: 24px;">
LOADING
</div>
<div id="result">
<script>
function xxx(){
$('#result').hide();
$('#overlay').show();
$.ajax({
url: 'test.php',
type: 'POST',
data: $('#f1').serialize(),
success: function(data){
$('#overlay').hide();
$('#result').show();
$('#result').html(data);
}
});
return false;
}
</script>
</body>
</html>
$(document).ready(function() {
function xxx() {
$('#result').hide();
$('#overlay').show();
$.ajax({
url: 'test.php',
type: 'POST',
data: $('#f1').serialize(),
success: function(data) {
$('#overlay').hide();
$('#result').show();
$('#result').html(data);
}
});
return false;
}
xxx(); // called when page is loaded.
$("form#f1").submit(function(event) {
xxx(); // call function when form is submitted.
event.preventDefault();
});
});
//when the page is loaded
$.ready(function(){
// call of your function
xxx();
$('#f1').submit(function(){
// new call of your function
xxx();
});
});
这样做:
$(document).ready(function(){
function xxx(){
$('#result').hide();
$('#overlay').show();
$.ajax({
url: 'test.php',
type: 'POST',
data: $('#f1').serialize(),
success: function(data){
$('#overlay').hide();
$('#result').show();
$('#result').html(data);
}
});
return false;
}
xxx();
$("form#f1").submit(function(e){
e.preventDefault();
xxx();
});
});
或者,如果你不想阻止你的表格提交,请使用这个:
$(document).ready(function(){
xxx();
$("form#f1").submit(xxx);
});
您可以在中包装您的JavaScript
$(document).ready( function() {
/* JS HERE */
});
这只适用于DOM就绪的情况。
我在HTML中看不到提交按钮,但您可以在该按钮上绑定一个点击事件,然后执行一个函数。
在页面加载时,只需在document.ready
:上运行该方法
$(xxx);
对于表单提交,将其挂接到表单提交事件:
$("#f1").on("submit", xxx);
将本应运行的函数放在文档就绪函数中。
$(document).ready(function() {
your code here
});
无论何时加载并准备好文档,这都将调用一个函数。
事实上,你的表格中没有提交按钮,你可以进行
$("#overlay").click(function() {
your code here
});
每当单击id为#overlay的div时,这将运行函数内部的代码。
如果你想用jquery 加载
//Remember to use closure, always!!
(function ($) {
$(document).ready( function () {
function xxx () {
//Do your magic
}
xxx();
$('#your-form').submit(function (e) {
xxx();
//if you want to avoid the default submit
e.preventDefault();
});
})
})(jQuery);
相关文章:
- HTML表单提交时未执行外部函数
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 将图像上传ajax与表单提交ajax相结合
- Javascript无法处理表单提交
- 如何在我的情况下禁用表单提交
- 加载后的页面与ajax表单提交不起作用
- angularjs-控制器在表单提交时未调用
- 禁用带有字符白名单的表单提交
- 如何使用Google Analytics跟踪表单提交
- 使用javascript将表单提交到iframe目标中
- e.preventDefault在表单提交时被忽略
- AngularJS JQuery Ajax表单提交等效
- JavaScript表单提交帮助
- PHP表单提交和重定向
- 当所有输入文本字段都为空时,禁止表单提交,但当jquery中的任何字段不为空时允许提交
- setTimeOut AFTER jQuery表单提交
- 从函数中选择要触发表单提交的正确元素
- JavaScript表单提交没有't fire asp服务器端点击功能的提交按钮
- 表单提交问题,如何在我的URL末尾获得ID的值
- Javascript使用Confirm取消表单提交