我有一个文件输入,其中有两个函数f()
和colorit()
被调用。如果colorit()
中未满足指定条件,我想停止函数f()
。但是,只要选择了该文件,两个功能都会启动。
<body>
<div class="dClass">
<input type="file" class="iClass" />
</div>
<script>
function f(){
if () {
// if image selected does not meet requirements
// how to stop colorit(); function here ??
return false;
}
else {
alert("done");
}
}
$('.iClass').change(function() {
f(); // calling function f
});
$(function() {
$('.dClass').colorit();
//note: colorit() function also comes to action as soon as a file is selected through .iClass
});
</script>
</body>
答案 0 :(得分:0)
删除 -
$(function() {
$('.dClass').colorit();
}
如果colorit()
成功,您只希望f()
能够正常工作。
将f()
定义为 -
function f(obj){
if () { // if image selected does not meet requirements
// how to stop colorit(); function here ??
return false;
}
else {
obj.colorit();
alert("done");
}
}
这样,如果是sicceeds,您可以使用该对象启动colorit
。
并致电f()
,如 -
$('.iClass').change(function() {
f($('.dClass')); // calling function f
});
另一种方式是 -
function f(obj){
if () { // if image selected does not meet requirements
// how to stop colorit(); function here ??
return false;
}
else {
obj.data('fdone', 1);
alert("done");
}
}
并且
$('.iClass').change(function() {
f($('.dClass')); // calling function f
if($(this).data('fdone') == 1) {
$('.dClass').colorit();
}
});
和 -
<input type="file" class="iClass" data-fdone='0' />
答案 1 :(得分:0)
您可以强制停止执行函数,而不是重新编写,但这是一种方式
throw new Error('This is not an error. This is just to abort javascript');