简体   繁体   English

提交的文件不是提交表格

[英]document on submit isn't submitting form

I have a button outside of the form that when pressed, submits my form. 我在表单外有一个按钮,按下该按钮即可提交我的表单。 That form has an event on it for onSubmit that fires off and then just does some form checks, making sure fields are present. 该表单上有一个onSubmit事件,该事件将触发,然后仅进行一些表单检查,以确保存在字段。 At the end of the form, I return true and nothing happens. 在表单末尾,我返回true,什么也没有发生。 Can't seem to figure this out. 似乎无法弄清楚。 Appreciate the extra set of eyes. 欣赏额外的眼睛。 I did verify that that function is being called and makes it all the way past the checks, just nothing happens. 我确实验证了该函数已被调用,并使其一直通过检查,但是什么也没有发生。 Here's the code: 这是代码:

$(document).on('click','.but_addTask',function(e){
    e.preventDefault();
    $('#addTaskForm').submit();
});

$(document).on('submit','#addTaskForm',function(e){
    e.preventDefault();

    var description = $('#description').val();
    var dueDate = $('#dueDate').val();

    if(!$('.taskClientID').length){
        alert('Please add client(s) to task');
        $('#taskClientSearch').focus();
        return false;
    }

    if(description==""){
        alert("Please enter a description")
        $('#description').focus();
        return false;
    }

    if(!$('.taskAuditorID').length){
        alert('Please add owner(s) to task');
        $('#taskOwnersSearch').focus();
        return false;
    }

    if(dueDate==""){
        alert("Please enter a dueDate")
        $('#dueDate').focus();
        return false;
    }

    console.log('made it!');
    return true;

});

You already called e.preventDefault(); 您已经调用了e.preventDefault(); at the start of the callback, which suppresses the default behaviour of the event (in this case of course, that behaviour is to submit). 在回调的开始处,它抑制了事件的默认行为(在这种情况下,当然是要提交的行为)。 By the time you return true it's too late. 到您返回true时,为时已晚。

If you remove that line, you should be ok. 如果删除该行,则应该没问题。

Docs: https://api.jquery.com/event.preventdefault/ 文件: https//api.jquery.com/event.preventdefault/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM