获取表单onsubmit以从addEventListener函数返回true

时间:2011-12-08 01:28:11

标签: jquery forms submit

这是问题所在。我有一个包含各种输入和提交按钮的表单。当用户点击“提交”时,它等待“addEventListener”事件触发(通常在3秒内)。我需要表单然后正确提交表单(最好不使用AJAX)。

$('#form').submit(function(){
    var ele=document.getElementById('audio');
    ele.addEventListener('canplay',function(){
        // After doing some stuff here, how do I get the parent function to
        // return true so that the form can be submitted?
    });
    // I put this here so that the browser is forced to wait for the
    // "addEventListener" event
    return false;
});

1 个答案:

答案 0 :(得分:1)

存储一个布尔变量,指示是否提交表单。将其从true处理程序设置为canplay

(function () {
    var shouldSubmit = false;
    $('#form').submit(function(){ 
        var ele=document.getElementById('audio'); 
        ele.addEventListener('canplay',function(){ 
            // do some stuff here
            shouldSubmit = true;
        }); 
        return shouldSubmit; 
    }); 
})();

这将导致在canplay事件触发后,可以提交表单。如果您希望在canplay事件触发时立即提交表单,请改为呼叫form.submit()

$('#form').submit(function(){ 
    var ele=document.getElementById('audio'); 
    var form = this;
    ele.addEventListener('canplay',function(){ 
        // do some stuff here
        form.submit();
    }); 
    return false; 
});