[英]Calling a JQuery function inside a condition
I'm new to JS and JQuery and I'm trying implement the following functionality. 我是JS和JQuery的新手,正在尝试实现以下功能。
I have a form which has multiple inputs, each having its individual validation. 我有一个包含多个输入的表单,每个输入都有其单独的验证。 These validations have been implemented inside one function.
这些验证已在一个函数中实现。
function checkUpload() {
var flagu1=0;
var bookname = document.getElementById("title").value;
var flagu2=0;
.....
.....
var flagu6=0;
if( flagu1==0 && flagu2==0 && flagu3==0 && flagu4==0 && flagu6==0 )
return true;
else
return false;
}
This validation function is executed when a user clicks on the following button: 当用户单击以下按钮时,将执行此验证功能:
<button class="btn btn-secondaryup" name="submit" value="Upload" id="submitmeup" data-loading-text="UPLOADING..." onclick="clearBox('uploaderror')" type="submit" style="width:100%">UPLOAD</button>
Now I'm trying to implement Bootstap's 'loading state button' in this. 现在,我正在尝试在其中实现Bootstap的“加载状态按钮”。 The functionality would work like this:
该功能将像这样工作:
When a user clicks on the button, the front-end validation function is called. 当用户单击按钮时,将调用前端验证功能。 Once the validation function (checkUpload()) returns true, the following JQuery function should be called.
验证函数(checkUpload())返回true后,应调用以下JQuery函数。
<script>
$('#submitmeup').click(function() {
var $btn = $(this);
$btn.button('loading');
setTimeout(function () {
$btn.button('reset');
}, 5000);
});
</script>
The trick is that the button loading function has to be called only after the checkUpload function returns true. 诀窍是仅在checkUpload函数返回true之后才必须调用按钮加载函数。 How do I implement this?
我该如何实施?
use below code. 使用以下代码。 Use function inside condition .if function returns true then it execute code in side if condition
在条件内部使用函数。如果函数返回true,则在条件内部执行代码
<script>
$('#submitmeup').click(function() {
var $btn = $(this);
if(checkUpload()){
$btn.button('loading');
setTimeout(function () {
$btn.button('reset');
}, 5000);
}
});
</script>
First remove the onclick
attribute from the button: 首先从按钮中删除
onclick
属性:
<button class="btn btn-secondaryup" name="submit" value="Upload" id="submitmeup" data-loading-text="UPLOADING..." type="submit" style="width:100%">UPLOAD</button>
Then define your functions and set .on('click')
event handler: 然后定义您的函数并设置
.on('click')
事件处理程序:
$(function() {
var $submitButton = $('#submitmeup');
// Submit handler
function handleSubmit() {
clearBox('uploaderror');
var uploadValid = checkUpload();
if (uploadValid) {
// Upload successful
showButtonLoading();
}
}
function showButtonLoading() {
$submitButton.button('loading');
setTimeout(function () {
$submitButton.button('reset');
}, 5000);
}
function checkUpload() {
var flagu1=0;
var bookname = document.getElementById("title").value;
var flagu2=0;
.....
.....
var flagu6=0;
if( flagu1==0 && flagu2==0 && flagu3==0 && flagu4==0 && flagu6==0 )
return true;
else
return false;
}
function clearBox(type) {
// Your implementation here
}
// Event handler
$submitButton.on('click', handleSubmit);
});
Now, clicking the button will go to handleSubmit
function, which employs your code. 现在,单击按钮将转到
handleSubmit
函数,该函数将使用您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.