0

i'm having some crazy trouble and i can't figure out how to fix it. I have a form that has a "return submitNotes" on the submit button (onclick). i have the submitNotes function and it seems to be calling it fine, but in my function i have an ajax call that doesn't seem to be firing off. I've placed alerts in the ajax call for when it is finished and the curious thing is that the alert only goes off when i have return false at the bottom of the page. If i comment out the return it doesn't even show the alert. In both cases the ajax call doesn't seem to be reaching out to it's page. Anything you guys can do to help!

function submitNotes(rID){

    ts = getTs(); //this is so IE sees a unique URL being called everytime.
    var consignor = $('#consignor').val()
    var consignee = $('#consignee').val();
    var consignorRequired = $('#consignorRequired').val();

    if(consignorRequired=='true'){

            if(consignor==''){
                alert('Consignor information is required!');
                return false;
            }

            if(consignee==''){
                alert('Consignee information is required!');
                return false;
            }
    }

    $('#loading_notes').show();

    $.get('/scripts/claim_sendTo.asp?action=pending&rID='+rID+'&auditorID=1',function(){
        alert('hello2');
        $('#claimForm').submit();
    });

    //return false;

}
4

2 回答 2

0

您可能想要尝试的是阻止事件的默认行为。

您可能希望在表单上使用 onsubmit="" 而不是 onclick="",因为这样您就可以在核心处停止表单提交。

然后,您需要将事件对象传递给提交函数。它允许更好地控制事件,而无需过多依赖返回值。

<form method="post" action="X" onsubmit="submitNotes(event)">

// Somewhere in your JavaScript
function submitNotes(event) {
    if (event.preventDefault) event.preventDefault();
    else window.event.cancelBubble = true;
    // the rest of the event code
}

这应该是一个好的开始。

如果您想坚持使用该onclick事件,您可能需要尝试以下操作:

<form method="post" action="X" onsubmit="if (event.stopPropagation) event.stopPropagation(); else window.event.cancelBubble = true">

这应该会阻止表单处理提交。

我可以推荐你使用 jQuery 的$('form').submit( ... )事件处理吗?

于 2013-01-23T22:31:11.063 回答
0

当您取消注释时警报会消失,return false因为您处于 onsubmit 事件的上下文中。从该事件返回false将取消它,允许您的 jquery 执行。否则,表单会重新加载页面

于 2013-01-23T22:31:38.153 回答